当我尝试单击 gridview 中更新所选行的按钮时出现错误
代码和图像中的错误
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: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.net 按钮放入该网格视图中。
所以,这个:
<asp:Button id="cmdmyupdate" runat="server"
Text="Status Update"
OnClick="cmdmyupdate_Click"
>
</asp:Button>
那么,让我们创建点击事件(就像您对任何普通按钮所做的那样,对吧?)。
因此,输入时看起来像这样:(您需要/想要使用/让 intel-sense 为后面的代码创建按钮事件)。
好的,现在如果我们翻转到代码后面,我们就会点击按钮。
所以,现在你的代码可以是这样的:
我们需要从该行获取/抓取发票号码。
我们必须使用 .Cells() 集合(从零开始,因此发票编号是第二个,或“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