我正在使用带有 C# 的 ASP.NET 开发一个简单的基于 Web 的建议应用程序。它运作良好,除了一件事。我有以下数据库设计:
餐桌安全建议日志
ID, Title, Description, Username, DateSubmitted, TypeID, OtherTypes
表安全建议类型
ID, Type
用户将能够填写表格并提交。我可以将用户名、标题、描述和提交日期插入数据库。但我必须插入类型,要么是数据库中的类型之一,要么是新类型。我正在努力解决这部分问题,我不知道谁解决了它。那么你能帮我解决这个问题吗?如何将类型的值插入数据库?
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:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [SafetySuggestionsType]"/>
<asp:TextBox ID="TextBox1" runat="server" CssClass="text"/><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="dropdownlist"
ErrorMessage="Please select a type ... or choose Others"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="Please enter a type for your suggestion"/>
</li>
<li>
<label for="subject">Subject</label>
<asp:TextBox ID="txtSubject" runat="server" CssClass="text"/><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtSubject"
ErrorMessage="Please enter a subject/title for your suggestion"/>
</li>
<%--The following hidden field is for inserting the date--%>
<li>
<asp:TextBox ID="dateSubmitted" runat="server" CssClass="text"
Visible="false"/><br />
</li>
<li>
<label for="message">Your Suggestion</label>
<asp:TextBox ID="txtSuggestion" runat="server" cols="50"
CssClass="textarea" rows="6" TextMode="MultiLine"/><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtSuggestion"
ErrorMessage="Please enter your suggestion"/>
</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>
代码隐藏:
protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
{
SmtpClient sc = new SmtpClient("MAIL.Aramco.com");
StringBuilder sb = new StringBuilder();
MailMessage msg = null;
sb.Append("Message from: " + Service.User.Name + "\n");
sb.Append("Email: " + Service.User.GetProperty("EMP_BINTERMAIL") + "\n");
sb.Append("Type: ");
sb.Append((DropDownList.SelectedItem.Text.Equals("Others")
? TextBox1.Text : DropDownList.SelectedItem.Text) + "\n");
sb.Append("Title: " + txtSubject.Text + "\n");
sb.Append("Suggestion : " + txtSuggestion.Text + "\n");
try
{
msg = new MailMessage(Service.User.GetProperty("EMP_BINTERMAIL"),
"JOHN.ARNESON@ARAMCO.COM", "PSSP: New Safety Suggestion Box",
sb.ToString());
sc.Send(msg);
MultiView1.SetActiveView(ViewConfirm);
}
catch (Exception ex)
{
throw ex;
//Response.Write("Something bad happened!");
}
finally
{
if (msg != null)
{
msg.Dispose();
}
}
//For storing the suggestions in the database
string connString = "Data Source=localhost\\sqlexpress;"
+ "Initial Catalog=psspdbTest;Integrated Security=True";
string insertCommand = "INSERT INTO SafetySuggestionsLog "
+ "(Title, DateSubmitted, Description, Username) values "
+ "(@Title, @DateSubmitted, @Description, @Username)";
string username = Service.User.ID;
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();
}
}
}
更新:
我修改了存储建议信息的代码:
//For stroing the suggestions in the database
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
//string insertCommand = "INSERT INTO SafetySuggestionsLog (Title, DateSubmitted, Description, Username) values(@Title, @DateSubmitted, @Description, @Username)";
string insertCommand = @"insert into SafetySuggestionsLog
( Title, Description, Username, DateSubmitted, TypeID, OtherTypes ) values
( @Title, @Description, @Username, @DateSubmitted,
( select ID from SafetySuggestionsType where Type = @Type ), '?' )";
string username = Service.User.ID;
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("@Type", DropDownList.SelectedValue);
cmd.ExecuteNonQuery();
}
}
而且我仍然无法从下拉列表中存储所选类型。即使我选择了其中一种预定义类型,我在数据库中的 othertype 列下得到 (?),在 Type 列下得到 NULL。为什么?