Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataList Textbox Insert Issue (Is this possible?) 1

Status
Not open for further replies.

talenx

Programmer
Aug 7, 2001
157
US
Hello all, not too sure if I can do this or not but I have a datalist that i have populated with a series of dates, i also have placed a single textbox in the datalist under the date allowing to enter a value per date. Let’s say 12 days worth or date which can vary depending on a start date and a duration variable (like a drop down box with number values in it).
So back to the issue, Can I take to values per date within the datalist and submit them to the database (SQL Server)? For some reason I can't find any info is how to get the value of each dates textbox (seeing that every textbox is repeated per date and the textbox name is unique, how would I differentiate one from another?

Here is the code for the datalist
Code:
<form action="" runat="server" method="post">
<asp:DataList runat="server" id="DataList1" 
                Width="85%" Border="1"
                HorizontalAlign="Center"
                Font-Name="Verdana" CellPadding="1"
                Font-Size="10pt"
                RepeatColumns="14" >

<ItemTemplate> 
  <div align="center"><%# DataBinder.Eval(Container.DataItem, "Date_Key") %>
      <br>
  </div>
  <asp:TextBox ID="fld_projectedsale"  Columns="5" runat="server" /> </ItemTemplate>
<footertemplate> <asp:Button ID="btn_submit"  Text="Insert" runat="server" /></footertemplate>
</asp:datalist>
</form>

Any thoughts?
Thanks
TalenX



 
Create a Sub for your Button. Set your DataKeyField of the DataList to your Date_Key.

Code:
//C#
//Your Subroutine
//Connection,Command etc...
string sql="Update myTable SET someColumn = @dtmDate WHERE Date_Key = @Data_KeyID";
string Date_KeyID;
string txtBoxDate;
foreach(DataListItem dlItem in DataList1.Items)
{
  Date_KeyID = DataList1.DataKeys[dlItem.ItemIndex].ToString();
  txtBoxDate = (TextBox)(dlItem.FindControl("fld_projectedsale")).Text;

  //Create your parameters and do your update here
}
 
BTW:
Code:
<asp:DataList runat="server" id="DataList1" 
                Width="85%" Border="1"
                HorizontalAlign="Center"
                Font-Name="Verdana" CellPadding="1"
                Font-Size="10pt"
                RepeatColumns="14" 
                [b]DataKeyField="Date_Key"[/b]>
 
Hey Veep, thanks for the quick response. In looking at your code. I’m sorry to say I don't really understand C# I can sort of follow it and I barely have a grasp of VB but I think I understand what your trying to do.

Correct me if am wrong here but

this should loop through all elements in the datalist.
Code:
'C#
foreach(DataListItem dlItem in DataList1.Items)
'VB
For Each dlItem As DataListItem In DataList1.Items

But I think where I get confused is how you get the values of the textbox and date_key index and place them into a SQL string, (which in most cases will be a stored procedure).
Any thoughts.
Thanks
talenx




 
You've got the concept!
In VB.Net it should go something like this (VB guys please help me here if I'm wrong):
Code:
For Each dlItem As DataListItem In DataList1.Items
 Date_KeyID = DataList1.DataKeys(dlItem.ItemIndex).ToString
 txtBoxDate = CType((dlItem.FindControl("fld_projectedsale")).Text, TextBox)
' then we're going to assume that you already instantiated
' connection and command (we'll call it cmd) objects and opened your connection
'
cmd.Parameters.Add("@Date_KeyID",SqlDbType.Int).Value=Date_KeyID
cmd.Parameters.Add("@dtmDate",SqlDbType.DateTime).Value=txtBoxDate
cmd.ExecuteNonQuery 
Next
Basically for each DataList Item we're gonna extract the DataKey and the value of the text box and update the database with the value of the the text box where the DataKey has a match. Pretty wild huh?
 
Veep. Again thank you for all of your help I think I might be getting somewhere. Oddly I attempted to apply the above code to the project and got an error

BC30456: 'Text' is not a member of 'System.Web.UI.Control'.
[/color red]
I was looking at the 'System.Web.UI.Control' collection list on MS site and couldn't find an example as to how get the values from each item within a datalist. or maybe I just didn't see it.



 
BTW here is the rest of the code on the page.

Code:
<%@ Page Language="VB" ContentType="text/html" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<HTML>
	<HEAD>
		<title>Add New Truck</title>
		<script language="VB" runat="server">
Dim DSConnection As SqlConnection

Sub Page_Load(Sender As Object, E As EventArgs)
   
If Not IsPostBack Then
BindDropDowns()
Else 

   End IF
End Sub

Sub BindDropDowns()
'LOCATION
'set dims
        Dim DS_Location As DataSet
       ' Dim DSConn_Location As SqlConnection
        Dim Cmd_Location As SqlDataAdapter
'set up data connection via stored proceedure
        DSConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PSF_DataStore"))
        Cmd_Location = New SqlDataAdapter("Psf_sp_ActiveDateList_TEST", DSConnection)
		Cmd_Location.SelectCommand.CommandType = CommandType.StoredProcedure
'create new dataset
        DS_DateList = New DataSet()
        Cmd_DateList.Fill(DS_DateList, "Date")
'bind data to control
        DataList1.DataSource = DS_DateList.Tables("Date").DefaultView
        DataList1.DataBind()
End Sub

    Sub GetListItems(sender As Object, e As EventArgs)
        If DataList1.Items.Count > 0 Then
	'testing to see if i get any data.
            Label1.Text = "The Items collection contains: <br>"
            
            Dim dlItem As DataListItem
			Dim Date_KeyID As String 
			Dim txtBoxDate As String 
            For Each dlItem In  DataList1.Items
 			Date_KeyID = DataList1.DataKeys(dlItem.ItemIndex).ToString
 			txtBoxDate = CType((dlItem.FindControl("fld_projectedsale")).Text, TextBox)
            Next dlItem
        End If
    End Sub 'Button_Click
	
</script>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form action="" runat="server" method="post">
<asp:DataList CellPadding="1" DataKeyField="Date_Key" DataMember="Date_Key"
                Font-Name="Verdana"
                Font-Size="10pt"
                HorizontalAlign="Center" id="DataList1"
                RepeatColumns="14" runat="server" 
                Width="85%" Border="1" >

<ItemTemplate> 
  <div align="center"><a href="#">go<%# DataBinder.Eval(Container.DataItem, "Date_Key") %></a>
      <br>
  </div>
  <asp:TextBox ID="fld_projectedsale"  Columns="5" runat="server" /> </ItemTemplate>
<footertemplate> <asp:Button ID="btn_submit"   OnClick="GetListItems" Text="Insert" runat="server" /></footertemplate>
</asp:datalist>
</form>
<asp:Label id="Label1"
           runat="server"/>



</body>
</html>

Thanks
Talenx
 
Where is this error occurring?

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Sorry for the delay. I have been putting out fires...
Anyways I solved the last error issue thought I ran into another. I receive an error stating that Procedure or function PSFAP_spAddDateRange has too many arguments specified.
This only happens if there are more the one data items in the datalist. (Which is most every case it will have more the more data item.) though the routine does execute correctly if there is only one data item in the list ( meaning it inserts a record into the database table.)

Any thoughts.

Here is the code behind.
Code:
Public Sub GetListItems(ByVal sender As Object, ByVal e As EventArgs)

        Dim DSConnection As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("PSF_DataStore"))
        Dim Cmd_AdDateRange As New SqlCommand("PSFAP_spAddDateRange", DSConnection)

        Dim dItem As DataListItem
        Cmd_AdDateRange.CommandType = CommandType.StoredProcedure
        Try
            Dim Date_KeyID As Date
            Dim Date_Projection As TextBox
            Cmd_AdDateRange.Connection.Open()

            For Each dlItem As DataListItem In DataList1.Items
                Date_KeyID = (DataList1.DataKeys(dlItem.ItemIndex).ToString)

                Cmd_AdDateRange.Parameters.Add("@AdDate", CDate(Date_KeyID))
                Cmd_AdDateRange.ExecuteNonQuery()

            Next dlItem
            Cmd_AdDateRange.Connection.Close()


        Catch ex As SqlException
            Label1.Text = ex.Number & " - - " & ex.Message
        End Try


    End Sub 'Button_Click

Thanks
talenx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top