我知道这是一个古老的话题,但我找到的解决方案似乎都没有帮助。我是新来的,所以请指导我: 我正在调用数据库来检索记录,然后将其与复选框一起显示在gridview中。单击该复选框时,我需要将选中的行记录与insert语句中的其他查询信息一起保存到单独的表中。问题是firstname和lastname没有填充,导致null异常。任何人都可以查看这个并让我知道如何解决它?
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Class scores
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.GridView1.DataBind()
End Sub
Protected Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
For Each row As GridViewRow In GridView1.Rows
Dim chkrow As CheckBox = DirectCast(row.FindControl("cb1"), CheckBox)
If chkrow.Checked = True Then
Dim firstname As TextBox = DirectCast(row.FindControl("firstname"), TextBox)
Dim lastname As TextBox = DirectCast(row.FindControl("lastname"), TextBox)
Dim conn As New SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand()
conn.ConnectionString = "Data Source=SERVERNAME;Persist Security Info=True;User ID=ME;Password=PASSWORD;Initial Catalog=Testing"
cmd.Connection = conn
cmd.CommandText = "insert into scores (date, test, score, personid, testid) Values (getdate(), @test, @score, (select personid from persons where firstname = '" + firstname.Text + "' and lastname = '" + lastname.Text + "'), (select testid from tests where testnm ='" + DDL2.SelectedValue + "'))"
conn.Open()
Try
cmd.Parameters.Add("@test", SqlDbType.NVarChar).Value = DDL2.SelectedValue
cmd.Parameters.Add("@score", SqlDbType.NVarChar).Value = Score.Text
cmd.ExecuteNonQuery()
Response.Write("Saved")
Catch ex As System.Data.SqlClient.SqlException
MsgBox(ex.Message)
conn.Close()
End Try
Exit For
End If
Next
End Sub
结束班
标记页面:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="scores.aspx.vb" Inherits="scores" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Scores<br /><br />
Pick tester name <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="personid"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CB1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="personid" HeaderText="personid" InsertVisible="False" ReadOnly="True" SortExpression="personid" />
<asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" />
<asp:BoundField DataField="Lastname" HeaderText="Lastname" SortExpression="Lastname" />
<asp:BoundField DataField="Social" HeaderText="Social" SortExpression="Social" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Testing %>" SelectCommand="SELECT [personid], [Firstname], [Lastname], [Social] FROM [Persons] where [personid] not in (select personid from scores) ORDER BY [Lastname] "></asp:SqlDataSource>
Test taken <asp:DropDownList ID="DDL2" runat="server" DataSourceID="SqlDataSource2" DataTextField="TestNM" DataValueField="TestNM"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Testing %>" SelectCommand="SELECT [TestNM], [testid] FROM [Tests] ORDER BY [TestNM]"></asp:SqlDataSource>
Score <asp:TextBox ID="Score" runat="server"></asp:TextBox><br />
<asp:Button ID="Save" runat="server" Text="Save" />
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
在我尝试阅读之前,它看起来像是在重新绑定所有数据(在Page_Load中)。首先尝试检查回发:
If Not Page.IsPostback Then
Me.GridView1.DataBind()
End If