locked
Pull DataList Item into SqlCommand Insert Statement RRS feed

  • Question

  • User320184449 posted

    I have a datalist that returns multiple entries and I need to insert values via an OnClick command for a button I have with each entry.  Problem is it's saying that a label I have in the datalist is not declared, in particular the MyLabel.Text from my Insert statement below.  Can someone show me with code how I can declare this label or is it even possible?  When I put the label outside of the datalist, everything works fine but when it's inside it says it's not declared.  I've also tried using a gridview but it shows as not declared as well.

        Protected Sub mybutton_Click(ByVal sender As Object, ByVal e As Object)
    
            Dim cmd As New SqlCommand("INSERT INTO [myTable] ([SessionID], [myID]) VALUES ('" & Session.SessionID & "', '" & MyLabel.Text & "')", New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString.ToString()))
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
            Response.Redirect("mypage.aspx")
    
        End Sub
     
    Monday, June 15, 2009 1:25 PM

Answers

  • User1330954201 posted

     You should be able to find your label by grabbing the parent control of the button (the DataListItem) then using FindControl to look for your label.

      For example :-

     Protected Sub mybutton_Click(ByVal sender As Object, ByVal e As Object)
         

        Dim MyLabel As Label = DirectCast(sender, Button).Parent.FindControl("MyLabel")


     End Sub

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2009 6:22 PM

All replies

  • User848370396 posted

     you can't use the controls which are in the gridview or datalist,

    you need to find that control using findcontrol() method, then u need to use that.

    Label mylbl = (Label)DataList1.Items[datalist itemno].FindControl("MyLabel");

    then u can use that mylbl text property.

    in that above code, getting that datalist item no  depends on the scenarion and the event you are implementing.

     

    Monday, June 15, 2009 1:49 PM
  • User320184449 posted

    I've tried the findcontrol() method every way I can think of and I can't seem to get it to work.  I'm still kind of new to .NET, any chance you have code you can show me what you mean with?  I've been looking all over and I can't find any examples anywhere.

    Monday, June 15, 2009 2:01 PM
  • User848370396 posted

     http://dotnettipsandtricks.blogspot.com/

    http://www.myboogpages.com/2008/04/aspnet-adding-an-insert-command-to-datalist.html

    and also post your code so far what you did.

    Monday, June 15, 2009 2:09 PM
  • User1330954201 posted

     You should be able to find your label by grabbing the parent control of the button (the DataListItem) then using FindControl to look for your label.

      For example :-

     Protected Sub mybutton_Click(ByVal sender As Object, ByVal e As Object)
         

        Dim MyLabel As Label = DirectCast(sender, Button).Parent.FindControl("MyLabel")


     End Sub

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2009 6:22 PM
  • User320184449 posted

    Hey that works, thank you very much..

    Tuesday, June 16, 2009 4:44 AM