如何从列表框中选择多个项目到数据库 [英] how to select multiple items from listbox to database

查看:67
本文介绍了如何从列表框中选择多个项目到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[fname]
,[lname]
,[userid]
,[用户名]
,[密码]
,[确认密码]
,[gender]
,[位置]
,[语言] **我已使用语言列表框..如果我选择了多种语言,则应将其存储在语言领域的DB中..
,[mobileno]
,[mailid]
,[地址]
,[image]
从[sailam].[dbo].[注册..这是我的表格字段...

我的代码是:
使用系统;
使用System.Collections;
使用System.Configuration;
使用System.Data;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq;
使用System.Data.SqlClient;
使用System.IO;
使用System.Drawing;

公共部分类注册:System.Web.UI.Page
{

受保护的void Page_Load(对象发送者,EventArgs e)
{

}
受保护的void Button1_Click(对象发送者,EventArgs e)
{
int cnt = 0;
SqlConnection con =新的SqlConnection(数据源= RAJA-PC;初始目录= sailam;用户ID = sa;密码= sa123");
//string s = ConfigurationManager.ConnectionStrings ["con"].ConnectionString;
//SqlConnection con =新的SqlConnection(s);
con.Open();
SqlCommand cmd = new SqlCommand(插入寄存器值(""+ TextBox1.Text +"''," + TextBox2.Text +'',''" + TextBox3.Text +'','' "+ TextBox4.Text +",""+ TextBox5.Text +"''," + TextBox6.Text +'',""+ RadioButtonList1.SelectedItem.Text +"," "+ DropDownList1.SelectedItem.Text +"''," + ListBox1.SelectedItem.Text +'',""+ TextBox7.Text +"''," + TextBox8.Text +'', ''"+ TextBox9.Text +"'',''"+ FileUpload1.PostedFile.FileName +"''),con);

cmd.ExecuteNonQuery();


我已经使用了这段代码,但是只显示了多个语言字段项.其他字段值显示为空..

//string lang =插入寄存器(语言)值(@language)";
//string connstring =数据源= RAJA-PC;初始目录= sailam;用户ID = sa;密码= sa123";
/////SqlCommand cmd1 =新的SqlCommand(lang,con);
//cmd.Parameters.Add("@language,SqlDbType.VarChar,100);

//foreach(ListBox1.Items中的ListItem项目)
//{
//如果(item.Selected)
//{
//尝试
//{
//cmd.Parameters ["@ language"].Value = item.Text;
//cmd.ExecuteNonQuery();
//Label17.ForeColor = Color.Green;
//Label17.Text =已插入数据";
//}
//catch(ex ex例外)
//{
//Label17.Text = ex.Message;
//}
//}
//}


字符串文件名= Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("images/" +文件名));



con.Close();
如果(cnt == 0)
{
Label15.Text =已成功插入值";
}
其他
{
Label15.Text ="plz enter";
}
clear();
}
公共无效clear()
{
foreach(form1.Controls中的控件c1)
{
如果(c1是TextBox)
{
((TextBox)c1).Text =";
}
如果(c1是DropDownList)
{
(((DropDownList)c1).SelectedIndex = -1;
}
}
}
}

[fname]
,[lname]
,[userid]
,[username]
,[password]
,[confirmpassword]
,[gender]
,[location]
,[language]**i have used listbox for language..if i select multiple languages they should b stored in DB in the field of language..
,[mobileno]
,[mailid]
,[address]
,[image]
FROM [sailam].[dbo].[register..this is my table fields...

My code is:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;

public partial class registration : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
int cnt = 0;
SqlConnection con = new SqlConnection("Data source=RAJA-PC;Initial Catalog=sailam;User id=sa;Password=sa123");
//string s = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
//SqlConnection con = new SqlConnection(s);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into register values(''" + TextBox1.Text + "'',''" + TextBox2.Text + "'',''" + TextBox3.Text + "'',''" + TextBox4.Text + "'',''" + TextBox5.Text + "'',''" + TextBox6.Text + "'',''" + RadioButtonList1.SelectedItem.Text + "'',''" + DropDownList1.SelectedItem.Text + "'',''" + ListBox1.SelectedItem.Text + "'',''" + TextBox7.Text + "'',''" + TextBox8.Text + "'',''" + TextBox9.Text + "'',''" + FileUpload1.PostedFile.FileName + "'')",con);

cmd.ExecuteNonQuery();


i hav used this code but displaying multiple items of only language field..other fields values are showing null..

//string lang = "insert into register(language)values(@language)";
//string connstring = "Data source=RAJA-PC;Initial Catalog=sailam;User id=sa;Password=sa123";
////SqlCommand cmd1=new SqlCommand(lang,con);
//cmd.Parameters.Add("@language", SqlDbType.VarChar, 100);

//foreach (ListItem item in ListBox1.Items)
//{
// if (item.Selected)
// {
// try
// {
// cmd.Parameters["@language"].Value = item.Text;
// cmd.ExecuteNonQuery();
// Label17.ForeColor = Color.Green;
// Label17.Text = "Data Inserted";
// }
// catch (Exception ex)
// {
// Label17.Text = ex.Message;
// }
// }
//}


string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("images/" + filename));



con.Close();
if (cnt == 0)
{
Label15.Text = "values are inserted successfully";
}
else
{
Label15.Text = "plz enter";
}
clear();
}
public void clear()
{
foreach (Control c1 in form1.Controls)
{
if (c1 is TextBox)
{
((TextBox)c1).Text = "";
}
if (c1 is DropDownList)
{
((DropDownList)c1).SelectedIndex = -1;
}
}
}
}

推荐答案

,您选择数据表中的所有数据库值(语言字段),并且该数据表与下拉框绑定,从而选择一种语言.
you select all a database value (language field) in datatable and this datatable bind with dropdown box those select a language.


这篇关于如何从列表框中选择多个项目到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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