我的数据库中有两个表如下:
建议表:ID,标题,描述,StatusID等等
SuggestionsStatus表:ID,状态
我正在使用GridView来显示建议,在最后一栏中我放了一个DropDownList来选择每个Suggestion的状态。现在,当我尝试通过从DropDownList中选择状态来更新其中一个提交的建议的状态时,该值已被选中,但未插入到数据库中(这意味着我仍然在StatusID列中有NULL值)建议表)我不知道为什么。
我的ASP.NET代码:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
width="950px" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2"
Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false"
DataTextField="Status" DataValueField="ID" AutoPostBack="true"
OnDataBound="DropDownList_DataBound">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username,
dbo.Divisions.DivisionShortcut
FROM dbo.employee INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode"
FilterExpression="[DivisionShortcut] like '{0}%'">
<FilterParameters>
<asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
<%--For the DropDownList--%>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>
我的代码隐藏:
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
int suggestionStatus = int.Parse(ddl.SelectedValue);
//For inserting the status in the database
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
string insertCommand = "INSERT INTO SafetySuggestionsLog (StatusID) values(@StatusID)";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(insertCommand, conn))
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@StatusID", suggestionStatus);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
我修改了DropDownList_SelectedIndexChanged(),如上所示,但它给了我以下错误:
无法将值NULL插入列'Title',表中 'psspdbTest.dbo.SafetySuggestionsLog';列不允许空值。 INSERT失败。声明已经终止。
那么我如何解决这个问题才能更新数据库中(GridView)中提交的建议之一的状态?
答案 0 :(得分:2)
我认为您的操作必须在下拉列表值更改时完成吗?
为此,您必须使用OnSelectedIndexChanged
方法。当下拉列表中的值发生更改时,此方法将触发。在这里,您可以编写代码。
我认为你在这里没有提到你的数据库插入逻辑。 在asp下拉列表中,请添加此
<asp:DropDownList ID="DBList" runat="server" Height="20px" Width="226px" OnSelectedIndexChanged ="DBList_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
现在写入.cs文件
protected void DBList_SelectedIndexChanged(object sender, EventArgs e)
{
// inserting into database code here
}
答案 1 :(得分:0)
我建议您尝试插入表格 例如有4列 ID,标题,描述,StatusID 你试图插入statusid只忽略所有其他列和错误说标题列不允许为null 如果你想允许它接受null 所以从表设计设置允许null true到标题列