如何将此表单中的选定值插入数据库?

时间:2012-11-19 09:35:59

标签: c# asp.net sql-server-2008-r2

我是使用C#的ASP.NET新开发人员。我开发了一个基于Web的Intranet建议应用程序,它作为我所在部门员工提交的建议的池。

我有以下数据库设计:

Log Table: ID, Title, Description, DateSubmitted, Username, TypeID, StatusID
SuggestionsType Table: ID, Type
SuggestionsStatus Table: ID, Status
Employee Table: Username, Name, Title

我可以通过以下代码插入带有标题,提交日期,说明和用户名的建议:

ASP.NET代码

<div ID="contactform">
                <ol>
                    <li>
                        <label for="subject">
                        Type</label>
                        <asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource1" Width="155px" 
                                    Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false" 
                                    DataTextField="Type" DataValueField="ID" AutoPostBack="true" OnDataBound="DropDownList_DataBound">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                                    SelectCommand="SELECT * FROM [SafetySuggestionsType]"></asp:SqlDataSource>
                        <asp:TextBox ID="TextBox1" runat="server" CssClass="text"></asp:TextBox>
                        <br />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                            ControlToValidate="dropdownlist" 
                            ErrorMessage="Please select a type for your suggestion or choose Others and put a type for it"></asp:RequiredFieldValidator>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                            ControlToValidate="TextBox1" 
                            ErrorMessage="Please enter a type for your suggestion"></asp:RequiredFieldValidator>
                    </li>
                    <li>
                        <label for="subject">
                        Subject</label>
                        <asp:TextBox ID="txtSubject" runat="server" CssClass="text"></asp:TextBox>
                        <br />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                            ControlToValidate="txtSubject" 
                            ErrorMessage="Please enter a subject/title for your suggestion"></asp:RequiredFieldValidator>
                    </li>

                    <%--The following hidden field is for inserting the date--%>
                    <li>
                        <asp:TextBox ID="dateSubmitted" runat="server" CssClass="text" Visible="false"></asp:TextBox>
                        <br />
                    </li>

                    <li>
                        <label for="message">
                        Your Suggestion</label>
                        <asp:TextBox ID="txtSuggestion" runat="server" cols="50" CssClass="textarea" 
                            rows="6" TextMode="MultiLine"></asp:TextBox>
                        <br />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                            ControlToValidate="txtSuggestion" ErrorMessage="Please enter your suggestion"></asp:RequiredFieldValidator>
                    </li>
                    <li class="buttons">
                        <asp:ImageButton ID="imageField" runat="server" imageurl="images/Send.gif" 
                            OnClick="btnSubmit_Click" />
                        <%--<input type="image" name="imageField" id="imageField" src="images/Send.gif" />--%>
                    </li>
                </ol>
            </div>

C#代码:

string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=Test;Integrated Security=True";
        string insertCommand = "INSERT INTO Log (Title, DateSubmitted, Description, Username) values(@Title, @DateSubmitted, @Description, @Username)";
        string username = netID;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(insertCommand, conn))
            {
                cmd.Parameters.Clear(); 
                cmd.Parameters.AddWithValue("@Title", txtSubject.Text);
                cmd.Parameters.AddWithValue("@DateSubmitted", DateTime.Now.ToString());
                cmd.Parameters.AddWithValue("@Description", txtSuggestion.Text);
                cmd.Parameters.AddWithValue("@Username", username);
                cmd.ExecuteNonQuery();
            }
        }

我现在需要的是插入所选类型的值以便如何操作?

2 个答案:

答案 0 :(得分:2)

在插入查询中为seletedType添加一个参数:

string connString = "Data Source=dha00730-pmtq01\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        string insertCommand = "INSERT INTO SafetySuggestionsLog (Title, DateSubmitted, Description, Username, TypeID) values(@Title, @DateSubmitted, @Description, @Username,@TypeID)";
        string username = netID;
        int typeID = Convert.ToInt64(DropDownList.SelectedValue);
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(insertCommand, conn))
            {
                cmd.Parameters.Clear(); 
                cmd.Parameters.AddWithValue("@Title", txtSubject.Text);
                cmd.Parameters.AddWithValue("@DateSubmitted", DateTime.Now.ToString());
                cmd.Parameters.AddWithValue("@Description", txtSuggestion.Text);
                cmd.Parameters.AddWithValue("@Username", username);
                cmd.Parameters.AddWithValue("@TypeID",typeID);    
                cmd.ExecuteNonQuery();
            }
        }

答案 1 :(得分:0)

解决方案与@Rohit Vyas在其解决方案中提到的解决方案相同,但我只修改了以下代码行:

cmd.Parameters.AddWithValue("@TypeID",typeID); 

以下内容:

cmd.Parameters.AddWithValue("@TypeID",DropDownList.SelectedIndex);