从ListBox插入多个数据到Sql Table. [英] Inserting multiple data from ListBox into Sql Table.

查看:157
本文介绍了从ListBox插入多个数据到Sql Table.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我要将数据绑定到具有DataTextField和DataValueField的 ListBox中.我要插入 datavaluefield 进入具有多个数据的数据库.我正在尝试执行此操作,但它会插入 datatextfield .
这是我的代码:

Here I am binding data into a ListBox with DataTextField and DataValueField. I want to insert the datavaluefield into database with multiple data. I am trying to do this but it inserts the datatextfield.
Here is my code :

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection(str);
       string s1 = "select * from userlogin";
       SqlCommand cmd = new SqlCommand(s1, con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds);
       ListBox1.DataTextField = "user_name";
       ListBox1.DataValueField = "user_id";
       ListBox1.DataSource = ds;
       ListBox1.DataBind();
   }


protected void btnsubmit_Click(object sender, EventArgs e)
   {
       for (int i = 0; i < ListBox1.Items.Count; i++)
       {
           if (ListBox1.Items[i].Selected)
           {
               string st1 = ListBox1.Items[i].Text;
               string st2 = "insert into AssignRole values('"+st1+"','"+DropDownList1.SelectedValue.ToString()+"')";
               int k=cs1.Runsql(st2);
               if (k > 0)
               {
                   Label1.Text = "Assign Role Successfully";
               }
               else
               {
                   Label1.Text = "Assigning Failed";
               }
           }
       }
   }



我想将列表框的DataValueField插入数据库.我的代码应进行哪些更改.请帮助我.



I want to insert the DataValueField of listbox into database. What should be the changes in my code. Please help me.

推荐答案




根据我对您的代码行的理解,

Hi,


As per my understanding from your code line,

string st2 = "insert into AssignRole values(''"+st1+"'',''"+DropDownList1.SelectedValue.ToString()+"'')";




您正在串联列表框选定项的文本和下拉列表选定的值.

您要在哪里获取列表框选定的值?
如果要在"St1"变量中获取列表框所选项目的值,请使用-




You are concatenating the listbox selected item''s text and a dropdowns selected value.

where are you trying to fetch the listbox selected value?
If you want to get listbox selected item value in ''St1'' variable, use -

ListBox1.Items[i].Value




希望这会有所帮助.




Hope this helps.


这篇关于从ListBox插入多个数据到Sql Table.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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