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.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()
princyeliasAsked:
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.

nayernaguibCommented:
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
princyeliasAuthor 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()
toocrazy007Commented:
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.

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
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
Programming

From novice to tech pro — start learning today.