0

I have error when i try to click button in gridview that will update selected row

Code and the error in the image

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



    If Not Page.IsPostBack Then
        Try
            Dim con As New SqlConnection
            Dim cmd As New SqlCommand
            Dim dt As New DataTable()

            con.ConnectionString = "Data Source=DESKTOP-L7LKJH5;Initial Catalog=logicist;Integrated Security=True"
            con.Open()

            cmd.CommandText = "select * from dalabaad where Status= @Status"
            cmd.Parameters.AddWithValue("@Status", "Pending".ToString)
            cmd.Connection = con

            Datagrid1.DataSource = cmd.ExecuteReader
            Datagrid1.DataBind()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If

    depart()
End Sub

Protected Sub Datagrid1_click(sender As Object, e As GridViewCommandEventArgs)
    ' Update button
    If (e.CommandName = "Select") Then
        Try
            Con.ConnectionString = "Data Source=DESKTOP-L7LKJH5;Initial Catalog=logicist;Integrated Security=True"
            Dim sqlText = "UPDATE dalabaad SET Status = @Status WHERE Invoice_No = '" + Datagrid1.SelectedIndex + "'"
            Using cnConnect = New SqlConnection(Con.ConnectionString)
                Using cm = New SqlCommand(sqlText, cnConnect)
                    cnConnect.Open()
                    cm.Parameters.Add("@Status", SqlDbType.NVarChar).Value = "Approved".ToString

                    cm.ExecuteNonQuery()
                    MsgBox("Updated")
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If


End Sub

Asp.net vb code

        <asp:GridView ID="Datagrid1" runat="server" AutoGenerateColumns="False" Width="860px" OnRowCommand="Datagrid1_click" >
<Columns>
    <asp:BoundField DataField="RefNo" HeaderText="RefNo" ReadOnly="True" />
    <asp:BoundField DataField="Invoice_No" HeaderText="Invoice_No" />
    <asp:BoundField DataField="Item_Name" HeaderText="ITem Name" />
    <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
    <asp:BoundField DataField="Department" HeaderText="Department" />
    <asp:BoundField DataField="ReceiverName" HeaderText="Receiver Name" />
    <asp:BoundField DataField="Date" HeaderText="Date" />
    <asp:BoundField DataField="Approval_By" HeaderText="Approval_By" />
    <asp:BoundField DataField="Status" HeaderText="Status" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button runat="server"  Text="Button" CommandName="Select"  ></asp:Button>
        </ItemTemplate>
    </asp:TemplateField>

</Columns>

</asp:GridView>

HTML Code for Gridview

Object reference not set to an instance of an object.

Error message when i click the button in Gridview

I have tried to change places several times and it didn't work for me

1 Answer 1

0

It's best to drop in a plain asp.net button into that gridview.

So, this:

       <asp:Button id="cmdmyupdate" runat="server"  
            Text="Status Update" 
            OnClick="cmdmyupdate_Click"
        >
        </asp:Button>

So, let vs create the click event (like you do for ANY plain button, right?).

So, it looks like this while typing: (you need/want to use/let intel-sense create the button event for code behind).

enter image description here

OK, so now if we flip to code behind, we have our button click.

So, now your code can be this:

We need to get/grab the invoice number from that row.

We have to use .Cells() collection (starts at zero, so invoice num is 2nd one, or "1").

Protected Sub cmdmyupdate_Click(sender As Object, e As EventArgs)


    Dim btn As Button = sender
    Dim gvRow As GridViewRow = btn.NamingContainer
    Dim sInvoice As String = gvRow.Cells(1).Text

    Dim scon As String =
       "Data Source=DESKTOP-L7LKJH5;Initial Catalog=logicist;Integrated Security=True"

    Dim sqlText As String =
       "UPDATE dalabaad SET Status = @Status WHERE Invoice_No = @InvoiceNum"
    Using cnConnect = New SqlConnection(scon)
        Using cm = New SqlCommand(sqlText, cnConnect)
            cnConnect.Open()
            cm.Parameters.Add("@Status", SqlDbType.NVarChar).Value = "Approved"
            cm.Parameters.Add("@InvoiceNum", SqlDbType.NVarChar).Value = sInvoice
            cm.ExecuteNonQuery()
        End Using
    End Using

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.