如何将DropDownList的值插入数据库? [英] How to insert that value of DropDownList to the Database?

查看:47
本文介绍了如何将DropDownList的值插入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有两个表,如下所示:

建议表:ID,标题,描述,StatusID ...等

建议状态表:ID,状态


我正在使用GridView来显示建议,在最后一列中放置了一个DropDownList,用于选择每个建议的状态.现在,当我尝试通过从DropDownList中选择状态来更新已提交建议之一的状态时,该值已被选择,但并未插入数据库中(这意味着StatusID列中的值仍然为NULL)在建议表"中),我不知道为什么.

我的ASP.NET代码:

I have two tables in my database as following:

Suggestions Table: ID, Title, Description, StatusID... etc

SuggestionsStatus Table: ID, Status


I am using GridView to show the Suggestions and in the last column I put a DropDownList that for selecting the status for each Suggestion. Now, when I tried to update the Status of one of the submitted suggestions by selecting the status from the DropDownList, the value has been selected but it wasn''t inserted to the Database (which means still I have NULL value in the StatusID column in the Suggestions Table) and I don''t know why.

My ASP.NET code:

<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>





我的后台代码:





My Code-Behind:

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 updateCommand = "UPDATE SafetySuggestionsLog  SET [StatusID] = @StatusID WHERE [ID] = '" + index + "'";
    using (SqlConnection conn = new SqlConnection(connString))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
        {
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@StatusID", suggestionStatus);
            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }
}
}





****

我如上所述修改了 DropDownList_SelectedIndexChanged(),但它给了我以下错误:

无法将值NULL插入表"psspdbTest.dbo.SafetySuggestionsLog"的标题"列中;列不允许为空. INSERT失败.
该声明已终止.


那么,如何解决此问题,以便能够更新数据库(GridView)中已提交建议之一的状态?**





** **

I modified the DropDownList_SelectedIndexChanged() as shown above but it gave me the following error:

Cannot insert the value NULL into column ''Title'', table ''psspdbTest.dbo.SafetySuggestionsLog''; column does not allow nulls. INSERT fails.
The statement has been terminated.


So how I can fix this problem to be able to update the status of one of the submitted Suggestions in the (GridView) in the database?**

推荐答案

ConnectionStrings:testConnectionString%> SelectCommand ="SELECT dbo.SafetySuggestionsLog.ID,dbo.SafetySuggestionsLog.Title,dbo.SafetySuggestionsLog.Description,dbo.employee.Name,dbo.SafetySuggestionsLog.Username, dbo.Divisions.DivisionShortcut 来自dbo.employee内部联接 dbo.SafetySuggestionsLog on dbo.employee.Username = dbo.SafetySuggestionsLog.Username内联接 dbo.Diloys ON dbo.employee.DivisionCode = dbo.Divisions.SapCode FilterExpression ="[DivisionShortcut]之类的"{0}%""> < FilterParameters> < asp:ControlParameter ControlID ="ddlDivision" Name ="DivisionShortcut" PropertyName ="SelectedValue" Type ="String"/> </FilterParameters> </asp:SqlDataSource> <%-对于DropDownList-%> < asp:SqlDataSource ID ="SqlDataSource2" runat =服务器" 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>
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>





我的后台代码:





My Code-Behind:

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 updateCommand = "UPDATE SafetySuggestionsLog  SET [StatusID] = @StatusID WHERE [ID] = '" + index + "'";
    using (SqlConnection conn = new SqlConnection(connString))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
        {
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@StatusID", suggestionStatus);
            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }
}
}





****

我如上所述修改了 DropDownList_SelectedIndexChanged(),但它给了我以下错误:

无法将值NULL插入表"psspdbTest.dbo.SafetySuggestionsLog"的标题"列中;列不允许为空. INSERT失败.
该声明已终止.


那么,如何解决此问题,以便能够更新数据库(GridView)中已提交建议之一的状态?**





** **

I modified the DropDownList_SelectedIndexChanged() as shown above but it gave me the following error:

Cannot insert the value NULL into column ''Title'', table ''psspdbTest.dbo.SafetySuggestionsLog''; column does not allow nulls. INSERT fails.
The statement has been terminated.


So how I can fix this problem to be able to update the status of one of the submitted Suggestions in the (GridView) in the database?**


检查查询是否正确,并正确插入值.
Check that your query is correct and you are inserting the values appropriately.


这篇关于如何将DropDownList的值插入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆