通过循环列表框添加到数据库中选择值 [英] Looping through ListBox to add selected values to database

查看:104
本文介绍了通过循环列表框添加到数据库中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我想从列表框中添加多个选择的值到数据库中。多个测试名被选择用于每一个病人(取决于要求)和它们将被插入到指定的柱

Ok, so i am trying to add the multiple selected values from the ListBox into the Database. Multiple test names are to be selected for each patient (depending upon the requirements) and they are to be inserted into the column named

测试名。

Forexample:如果我选择ANA,CBC的病人,应该针对患者姓名的测试名列添加这两个值。
我曾尝试在互联网上提出了一些事情,但我没有成功。我张贴我的相关asp.net code与C#code(我最新的尝试)一起。

Forexample: If i select ANA, CBC for a patient, it should add BOTH values in the TESTNAME column against that patient name. I have tried a few things suggested on the internet but i have been unsuccessful. I am posting my related asp.net code along with C# code (my latest try).

以最新的尝试,它抛出一个错误说变量名@TestName'已声明。变量名必须是查询批处理或存储过程中是独一无二的。

请注意,我是初学者所以一些解释,也是应很多AP preciated。 asp.net code为GRIDVIEW:

Please note that i am a beginner so some explanation as well shall be very much appreciated. asp.net code for GRIDVIEW:

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

列表框的aspx code:

ListBox aspx code:

<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#code:

my C# code:

 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();
    }

也许我需要追加或连接或东西。但我不知道该怎么做。任何帮助将大大AP preciated。

Maybe i need to append or concatenate or something. But i am not sure how to do that. Any help will be greatly appreciated.

推荐答案

您不能添加两次相同的参数名称。您提到:

You cannot add the same parameter name twice. You mentioned:

应在测试名列中添加这两个值

should add BOTH values in the TESTNAME column

所以我想你想连接所选的值,如果这样,那么你可以这样做:

so I suppose you want to concatenate the selected values, if so then you can do something like:

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

这篇关于通过循环列表框添加到数据库中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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