Itemcommand to add a row in a Datalist to a new table

I have a Datalist that displays records from an XML file.  I have added a linkbutton with a specified CommandName in the Itemtemplate.  When clicked I want the associated record added to a table in a SQL database.  The Insert is working fine except rather than the selected row being inserted, EVERY record is being inserted.  How do I specify only one row be inserted to the table?
rpm962Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan_BarreraCommented:
Hi rpm962,

Can you please post the code you use for inserting the row?

Thanks,
0
rpm962Author Commented:
Hey Juan, sorry this took so long.  Thanks for your help.
Protected Sub dlShowLines_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlShowLines.ItemCommand
        
        If e.CommandName = "AddAwayTeam" Then
            Dim conn As String = ConfigurationManager.ConnectionStrings("LocktoberConnect").ConnectionString()
            Dim connection As SqlConnection = New SqlConnection(conn)
            For Each dl1 As DataListItem In dlShowLines.Items
                Dim sql As String = "INSERT INTO Picks2 ([Team], [Line], [DatePicked], [DateGame], [UserId], [Opponent]) VALUES (@Team, @Line, @DatePicked, @DateGame, @UserId, @Opponent)"
                Dim cmd As SqlCommand = New SqlCommand(sql, connection)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@Team", CType(dl1.FindControl("lblAwayTeam"), Label).Text)
                cmd.Parameters.AddWithValue("@Line", CType(dl1.FindControl("lblAwayLine"), Label).Text)
                cmd.Parameters.AddWithValue("@DatePicked", DateTime.Now)
                cmd.Parameters.AddWithValue("@DateGame", CType(dl1.FindControl("lblDateGame"), Label).Text)
                cmd.Parameters.AddWithValue("@UserId", UserIDValue.Text)
                cmd.Parameters.AddWithValue("@Opponent", CType(dl1.FindControl("lblHomeTeam"), Label).Text)
                connection.Open()
                cmd.ExecuteNonQuery()
                connection.Close()
            Next
        End If
    End Sub

Open in new window

0
Juan_BarreraCommented:

 Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
        If e.CommandName = "AddAwayTeam" Then
            Dim dl1 As DataListItem = e.Item.DataItem
            Dim conn As String = ConfigurationManager.ConnectionStrings("LocktoberConnect").ConnectionString()
            Dim connection As SqlConnection = New SqlConnection(conn)
            Dim sql As String = "INSERT INTO Picks2 ([Team], [Line], [DatePicked], [DateGame], [UserId], [Opponent]) VALUES (@Team, @Line, @DatePicked, @DateGame, @UserId, @Opponent)"
            Dim cmd As SqlCommand = New SqlCommand(sql, connection)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@Team", CType(dl1.FindControl("lblAwayTeam"), Label).Text)
            cmd.Parameters.AddWithValue("@Line", CType(dl1.FindControl("lblAwayLine"), Label).Text)
            cmd.Parameters.AddWithValue("@DatePicked", DateTime.Now)
            cmd.Parameters.AddWithValue("@DateGame", CType(dl1.FindControl("lblDateGame"), Label).Text)
            cmd.Parameters.AddWithValue("@UserId", UserIDValue.Text)
            cmd.Parameters.AddWithValue("@Opponent", CType(dl1.FindControl("lblHomeTeam"), Label).Text)
            connection.Open()
            cmd.ExecuteNonQuery()
            connection.Close()
        End If
    End Sub

Open in new window

0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

rpm962Author Commented:
I am getting "Object reference not set to an instance of an object"
Line 58:             cmd.Parameters.AddWithValue("@Team", CType(dl1.FindControl("lblAwayTeam"), Label).Text)
 
 

Open in new window

0
Juan_BarreraCommented:
Sorry rpm962, try by replacing:

Dim dl1 As DataListItem = e.Item.DataItem

with:

Dim dl1 As DataListItem = e.Item


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rpm962Author Commented:
Great!  Thank you.  I originally had it like your first suggestion with the .DataItem.  Turns out I wasted two days on 9 keystrokes.  Thank again!
0
Juan_BarreraCommented:
No worries!

Now, my initial mistake reminds me to make you a suggestion:
I've put  e.Item.DataItem becasue I'm used to work with the underlying data rather that with the presentation controls. If you can, use this instead of finding the controls and retrieving its value.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.