[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

update a row in datagrid

Posted on 2006-04-02
3
Medium Priority
?
218 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()
0
Comment
Question by:princyelias
3 Comments
 
LVL 14

Expert Comment

by:nayernaguib
ID: 16358005
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
0
 

Author Comment

by:princyelias
ID: 16358284
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()
0
 
LVL 2

Accepted Solution

by:
toocrazy007 earned 120 total points
ID: 16358614
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question