我正在尝试使用gridview更新/插入多行。一切都正确显示,直到我按下订单按钮。这是我的错误:
"Object reference not set to an instance of an object"
问题似乎在这里,但我不确定: FloorstockOrder.IsRowModified(GridViewRow row)
这是我的代码: System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1565
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:rx_empConnectionString_PICUSER %>" SelectCommand="select ABS(CHECKSUM(NewID())) as ID, drug Drug, null Qty
from
floorstock_meds
where
area = @area" UpdateCommand="insert into floorstock_order
(drug, area, qty, order_date)
values (@drug, @area, @qty, getdate())">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="area"
PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter ControlID="GridView2" Name="drug"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList1" Name="area"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GridView5" Name="Qty"
PropertyName="SelectedValue" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
<div>
<asp:GridView ID="GridView5" runat="server"
AllowPaging="True"
DataSourceID="SqlDataSource4"
AutoGenerateColumns="False" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server"
AutoPostBack="true"
OnCheckedChanged="chkSelect_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID"/>
<asp:BoundField DataField="Drug" HeaderText="Drug"
SortExpression="Drug"/>
<asp:TemplateField HeaderText="Qty"
SortExpression="Qty">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server"
Text='<%# Bind("Qty") %>' ReadOnly="true"
ForeColor="Blue" BorderStyle="none"
BorderWidth="0px" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server"
OnClick="btnUpdate_Click" Text="Order" /><br />
Private strConnection As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Private tableCopied As Boolean = False
Private originalTable As DataTable
Protected Sub GridView5_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If Not tableCopied Then
originalTable = DirectCast(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
ViewState("originalValues") = originalTable
tableCopied = True
End If
End If
End Sub
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs)
originalTable = DirectCast(ViewState("originalValues"), DataTable)
For Each row As GridViewRow In GridView5.Rows
If IsRowModified(row) Then
GridView5.UpdateRow(row.RowIndex, False)
End If
Next
tableCopied = False
GridView5.DataBind()
End Sub
Protected Function IsRowModified(ByVal row As GridViewRow) As Boolean
Dim ID As Integer
Dim qty As String
Dim area As String
ID = Convert.ToInt32(GridView5.DataKeys(row.RowIndex).Value)
qty = DirectCast(row.FindControl("txtName"), TextBox).Text
area = DirectCast(row.FindControl("DropDownList1"), DropDownList).SelectedValue
Dim newRow As System.Data.DataRow = originalTable.[Select]([String].Format("ID = {0}", ID))(0)
If Not qty.Equals(newRow("qty").ToString()) Then
Return True
End If
If Not area.Equals(newRow("area").ToString()) Then
Return True
End If
Return False
End Function
Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim chkTest As CheckBox = DirectCast(sender, CheckBox)
Dim grdRow As GridViewRow = DirectCast(chkTest.NamingContainer, GridViewRow)
Dim txtname As TextBox = DirectCast(grdRow.FindControl("txtName"), TextBox)
If chkTest.Checked Then
txtname.[ReadOnly] = False
txtname.ForeColor = System.Drawing.Color.Black
Else
txtname.[ReadOnly] = True
txtname.ForeColor = System.Drawing.Color.Blue
End If
End Sub