princyelias
asked on
update a row in datagrid
how i update a row in datagrid? i use the following codes.but didn't get output.there was an error "Object reference not set to an instance of an object"
Private Sub dg2_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) Handles dg2.UpdateCommand
getcon() 'function to get the connection
Dim str As String
str = datagrid.DataKeys(e.Item.I temIndex)
Dim txt As New TextBox
Dim str1, str2 As String
str1 = CType(e.Item.FindControl(" txt"), TextBox).Text
str2 = "update Client set ClientId= " & str1 & " where CurLocation='" & str & "'"
Dim cmd As SqlCommand
cmd.CommandText = str2
cmd.Connection = con
closecon() 'function to close the connection
datagrid.EditItemIndex = -1
bindgrid()
Private Sub dg2_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
getcon() 'function to get the connection
Dim str As String
str = datagrid.DataKeys(e.Item.I
Dim txt As New TextBox
Dim str1, str2 As String
str1 = CType(e.Item.FindControl("
str2 = "update Client set ClientId= " & str1 & " where CurLocation='" & str & "'"
Dim cmd As SqlCommand
cmd.CommandText = str2
cmd.Connection = con
closecon() 'function to close the connection
datagrid.EditItemIndex = -1
bindgrid()
ASKER
i tried the codes.but i didn't get answer.an error occured in
str1 = CType(e.Item.FindControl(" txt"), TextBox).Text
unable to find the textbox.Is there any need to create a textbox in the HTML(code behind)
the code i'm using:
Private Sub datagrid_UpdateCommand(ByV al source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) Handles datagrid.UpdateCommand
getcon() 'function to get the connection
Dim str, str1, str2 As String
Dim txt As New TextBox
Dim dr As SqlDataReader
str = datagrid.DataKeys(e.Item.I temIndex)
str1 = CType(e.Item.FindControl(" txt"), TextBox).Text
str2 = "update Client set ClientId= " & str1 & " where CurLocation='" & str & "'"
Dim cmd As New SqlCommand
cmd.CommandText = str2
cmd.Connection = con
dr = cmd.ExecuteReader
closecon()
dr.Close()
datagrid.EditItemIndex = -1
bindgrid()
str1 = CType(e.Item.FindControl("
unable to find the textbox.Is there any need to create a textbox in the HTML(code behind)
the code i'm using:
Private Sub datagrid_UpdateCommand(ByV
getcon() 'function to get the connection
Dim str, str1, str2 As String
Dim txt As New TextBox
Dim dr As SqlDataReader
str = datagrid.DataKeys(e.Item.I
str1 = CType(e.Item.FindControl("
str2 = "update Client set ClientId= " & str1 & " where CurLocation='" & str & "'"
Dim cmd As New SqlCommand
cmd.CommandText = str2
cmd.Connection = con
dr = cmd.ExecuteReader
closecon()
dr.Close()
datagrid.EditItemIndex = -1
bindgrid()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Dim cmd As SqlCommand
with
Dim cmd As New SqlCommand
You should also add the statement
cmd.ExecuteNonQuery()
after setting the command properties. This will execute the command against the database. Note that the connection *must* be open before executing the command.
_______________
Nayer Naguib