We help IT Professionals succeed at work.

update a row in datagrid

princyelias
princyelias asked
on
Medium Priority
231 Views
Last Modified: 2010-04-17
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.DataGridCommandEventArgs) Handles dg2.UpdateCommand

        getcon()      'function to get the connection
        Dim str As String
        str = datagrid.DataKeys(e.Item.ItemIndex)
        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()
Comment
Watch Question

Replace the statement

        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

Author

Commented:
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(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) 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.ItemIndex)
        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()
Do you insert the text in the itemtemplate of datagrid and given the id as "txt". if so there is not need of created the instance of textbox in the codebehind.
check by tracing weather you are getting the right values into the strings str and str1.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.