我正在使用带有C#的ASP.NET开发一个简单的建议基于Web的应用程序。除了一件事,它运作良好。我有以下数据库设计:
表 SafetySuggestionsLog
ID, Title, Description, Username, DateSubmitted, TypeID, OtherTypes
表 SafetySuggestionsType
ID, Type
用户可以填写表格并提交。我可以将用户名,标题,描述和提交的日期插入到数据库中。但我必须插入类型,它是数据库中的一种类型或新的类型。我正在努力解决这个问题,我不知道是谁解决了这个问题。 你能帮我解决这个问题吗?如何将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。为什么?
答案 0 :(得分:1)
存储过程可以优雅地处理所涉及的步骤。
if not exists ( select 42 from SafetySuggestionsType where Type = @Type )
insert into SafetySuggestionsType ( Type ) values ( @Type )
insert into SafetySuggestionsLog
( Title, Description, Username, DateSubmitted, TypeID, OtherTypes ) values
( @Title, @Description, @Username, @DateSubmitted,
( select TypeId from SafetySuggestionsType where Type = @Type ), '?' )
答案 1 :(得分:0)
您可以使用SqlDbType指定参数类型,如下所示:
cmd.Parameters.Add("@Title",SqlDbType.VarChar, 20);
cmd.Parameters["@Title"].Value = txtSubject.Text;
答案 2 :(得分:0)
您可以创建类型列主键,当您在数据库中插入新类型时,将抛出异常。因此,在try块中插入记录。
try{
//Do your insertion, if a duplicate item was found, the insertion will not take place
}
catch{
//Do something is an value was inserted which already existed
}