好的,所以我尝试将ListBox中的多个选定值添加到数据库中。为每位患者选择多个测试名称(取决于要求),并将它们插入名为
的列中测试名。
例如:如果我为患者选择ANA,CBC,则应在TESTNAME列中针对该患者姓名添加BOTH值。 我在互联网上尝试了一些建议,但我没有成功。我发布了我的相关asp.net代码和C#代码(我的最新尝试)。
使用最新的尝试,它会抛出错误,说明变量名称' @ TestName'已经宣布。变量名在查询批处理或存储过程中必须是唯一的。
请注意我是初学者,所以我也非常感谢你的一些解释。 GRIDVIEW的asp.net代码:
<div id="mbody">
<div class="gview">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="gview" DataSourceID="SqlDataSource1" DataKeyNames="PID">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="PID" HeaderText="PID" InsertVisible="False" ReadOnly="True" SortExpression="PID" />
<asp:BoundField DataField="Pname" HeaderText="Pname" SortExpression="Pname" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
<asp:BoundField DataField="Consultant" HeaderText="Consultant" SortExpression="Consultant" />
<asp:BoundField DataField="TestName" HeaderText="TestName" SortExpression="TestName" />
<asp:BoundField DataField="RequestDate" HeaderText="RequestDate" SortExpression="RequestDate" />
<asp:BoundField DataField="ReportDate" HeaderText="ReportDate" SortExpression="ReportDate" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>" SelectCommand="SELECT [PID], [Pname], [Gender], [Consultant], [TestName], [RequestDate], [ReportDate] FROM [Patient]" DeleteCommand="DELETE FROM [Patient] WHERE [PID] = @PID" InsertCommand="INSERT INTO [Patient] ([Pname], [Gender], [Consultant], [TestName], [RequestDate], [ReportDate]) VALUES (@Pname, @Gender, @Consultant, @TestName, @RequestDate, @ReportDate)" UpdateCommand="UPDATE [Patient] SET [Pname] = @Pname, [Gender] = @Gender, [Consultant] = @Consultant, [TestName] = @TestName, [RequestDate] = @RequestDate, [ReportDate] = @ReportDate WHERE [PID] = @PID">
<DeleteParameters>
<asp:Parameter Name="PID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Pname" Type="String" />
<asp:Parameter Name="Gender" Type="String" />
<asp:Parameter Name="Consultant" Type="String" />
<asp:Parameter Name="TestName" Type="String" />
<asp:Parameter Name="RequestDate" Type="String" />
<asp:Parameter Name="ReportDate" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Pname" Type="String" />
<asp:Parameter Name="Gender" Type="String" />
<asp:Parameter Name="Consultant" Type="String" />
<asp:Parameter Name="TestName" Type="String" />
<asp:Parameter Name="RequestDate" Type="String" />
<asp:Parameter Name="ReportDate" Type="String" />
<asp:Parameter Name="PID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
ListBox aspx代码:
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Height="57px" Width="270px">
<asp:ListItem>ANA</asp:ListItem>
<asp:ListItem>ASMA</asp:ListItem>
<asp:ListItem>ASO-titres</asp:ListItem>
<asp:ListItem>ESR</asp:ListItem>
<asp:ListItem>CBC</asp:ListItem>
<asp:ListItem>Anti-double Stranded DNA ab</asp:ListItem>
</asp:ListBox>
我的C#代码:
protected void Button3_Click(object sender, EventArgs e)
{
string str = "update Patient set TestName=@TestName, RequestDate=@RequestDate ,ReportDate=@ReportDate, Consultant=@Consultant where PID = '" + TextBox7.Text + "'";
cmd = new SqlCommand(str, con);
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected)
{
cmd.Parameters.AddWithValue("@TestName", ListBox1.SelectedItem.Text);
}
}
cmd.Parameters.AddWithValue("@RequestDate", TextBox4.Text.ToString());
cmd.Parameters.AddWithValue("@ReportDate", TextBox5.Text.ToString());
cmd.Parameters.AddWithValue("@Consultant", TextBox6.Text);
con.Open();
int flag = cmd.ExecuteNonQuery();
if (flag == 1) //On successful updation, shows a popup message
{
string msg = "Operation Successful";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(msg);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
else if (flag == 0)
{
string msg1 = "Operation Unsuccessful";
System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
sb1.Append("<script type = 'text/javascript'>");
sb1.Append("window.onload=function(){");
sb1.Append("alert('");
sb1.Append(msg1);
sb1.Append("')};");
sb1.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb1.ToString());
}
con.Close();
}
也许我需要追加或连接或其他东西。但我不知道该怎么做。任何帮助将不胜感激。
答案 0 :(得分:2)
您不能两次添加相同的参数名称。你提到过:
应在TESTNAME列中添加BOTH值
所以我想你想要连接选定的值,如果是这样的话你可以这样做:
var items = new List<string>();
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected)
{
items.Add(li.Text);
}
}
cmd.Parameters.AddWithValue("@TestName", string.Join(",", items));