I have a datalist within a datagrid and I am having trouble trying to get the
update command working. I am only using the one for the datagrid, but this
should also update the datalist, however when it gets to the section:
strgame_score = game_score1.Text it says Object reference not set to an
instance of an object.
Here is my code, can anyone help?
'-------------------------
' Create an appropriate UPDATE statement
strSQL = "UPDATE tblgame SET game_score = @gamescore WHERE game_id = @game_ID"
objconn.Open()
Dim game_ID, strgame_score As String
Dim Dlgame1 As DataList =
CType(e.Item.Cells(3).FindControl("DLscores_edit") , DataList)
' iterate through the DataList's TextBoxes
Dim dlItem As DataListItem
For Each dlItem In Dlgame1.Items
' reference the TextBox
Dim game_score1 As TextBox = CType(dlItem.FindControl("tbgamescore_edit"),
TextBox)
' Grab the value
strgame_score = game_score1.Text
' Get the title_id from DataKeys
game_ID = Dlgame1.DataKeys(dlItem.ItemIndex).ToString()
' Now, update the parameters
Dim game_scoreParam As New Data.SqlClient.SqlParameter("@game_score",
SqlDbType.Int)
If strgame_score = [String].Empty Then
' we need to insert a NULL
game_scoreParam.Value = DBNull.Value
Else
game_scoreParam.Value = strgame_score
End If
objcmd.Parameters.Add(game_scoreParam)
Dim game_IDParam As New Data.SqlClient.SqlParameter("@game_ID", SqlDbType.Int)
game_IDParam.Value = game_ID
objcmd.Parameters.Add(game_IDParam)
Me.Lbsession.Text = strSQL.ToString
' now, execute the query
objcmd.ExecuteNonQuery()
' Clear the parameters
objcmd.Parameters.Clear()
Next dlItem
objConn.Close() ' close the connection
'---------------