如何在Dropdownlist asp.net c#sqlerver中连接两列 [英] how to concatenate two columns in Dropdownlist asp.net c# sqlerver
问题描述
嗨frnds,
正在使用asp.net c#SqlServer。
我想要从sqlServer表连接两列(DeptUserName,Dept_Code)并显示在下拉列表中。
在下拉列表中它应显示为
------选择---------
Sandeep - IT
Ranjith - 财务
Sukumar - 物流
这里Sandeep是DeptUserName,IT是Dept_Code。
那么如何连接...
这是我的代码。
受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
FillDepartments();
}
}
private void FillDepartments()
{
//con.Open();
SqlCommand cmdFill = new SqlCommand(SELECT [DeptUserName] +' '+''+ [Dept_code]作为创建者来自Depts Order By Dept_code,con);
SqlDataAdapter daFill = new SqlDataAdapter(cmdFill);
DataSet ds = new DataSet ();
daFill.Fill(ds);
ddlDepartment.DataSource = ds;
ddlDepartment.DataTextField =DeptUserName;
ddlDepartment.DataValueField = Dept_code
ddlDepartment.DataBind();
ddlDepartment.Items.Insert(0,new ListItem(---- Select部门----,0));
//con.Close();
}
请帮助您。
谢谢。
private void FillDepartments()
{
//con.Open();
SqlCommand cmdFill = new SqlCommand(SELECT [DeptUserName] +''+''+ [Dept_code]作为创建者来自Dept Order By Dept_code,con);
SqlDataAdapter daFill = new SqlDataAdapter(cmdFill);
DataSet ds = new DataSet();
daFill.Fill(ds);
ddlDepartment.DataSource = ds;
ddlDepartment.DataTextField =DeptUserName+ - +Dept_code;
ddlDepartment.DataValueField = Dept_code
ddlDepartment.DataBind();
ddlDepartment.Items.Insert(0,new ListItem(---- Select Department ----,0));
//con.Close();
}
使用下面的代码
private void FillDepartments()
{
SqlCommand cmdFill = < span class =code-keyword> new SqlCommand( SELECT [DeptUserName] +' - '+ [dept_code],Dept_Code作为创建者来自Dept Order By Dept_code,con);
SqlDataAdapter daFill = new SqlDataAdapter(cmdFill);
DataSet ds = new DataSet();
daFill.Fill(ds);
ddlDepartment.DataSource = ds;
ddlDepartment.DataTextField = 创建者;
ddlDepartment.DataValueField = Dept_code;
ddlDepartment.DataBind();
ddlDepartment.Items.Insert( 0 , new ListItem( ----选择部门----, < span class =code-string> 0));
}
使用此代码填写
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(GetConnectionString());
尝试
{
connection.Open();
string sqlStatement = SELECT DeptUserName +' - '+ Dept_code AS Creator FROM Depts';
SqlCommand sqlCmd = new SqlCommand(sqlStatement,connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0 )
{
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = DeptUserName;
DropDownList1.DataValueField = Dept_code;
DropDownList1.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = 获取错误:;
msg + = ex.Message;
throw new 异常(msg);
}
最后
{
connection.Close();
}
}
Hi frnds,
am working on asp.net c# SqlServer.
I want to Concatenate two columns(DeptUserName,Dept_Code) from sqlServer Table and display in dropdownlist.
In dropdownlist it should appear as
------Select---------
Sandeep - IT
Ranjith - Finance
Sukumar - Logistics
here Sandeep is DeptUserName and IT is Dept_Code.
So how to Concatenate...
This is my code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDepartments();
}
}
private void FillDepartments()
{
//con.Open();
SqlCommand cmdFill = new SqlCommand("SELECT [DeptUserName] + ' ' + ' ' + [Dept_code] as Creator from Depts Order By Dept_code", con);
SqlDataAdapter daFill = new SqlDataAdapter(cmdFill);
DataSet ds = new DataSet();
daFill.Fill(ds);
ddlDepartment.DataSource = ds;
ddlDepartment.DataTextField = "DeptUserName";
ddlDepartment.DataValueField = Dept_code
ddlDepartment.DataBind();
ddlDepartment.Items.Insert(0, new ListItem("---- Select Department ----", "0"));
//con.Close();
}
Please your help is Required.
Thanks.
Make the following change in the C# Code:
private void FillDepartments() { //con.Open(); SqlCommand cmdFill = new SqlCommand("SELECT [DeptUserName] + ' ' + ' ' + [Dept_code] as Creator from Depts Order By Dept_code", con); SqlDataAdapter daFill = new SqlDataAdapter(cmdFill); DataSet ds = new DataSet(); daFill.Fill(ds); ddlDepartment.DataSource = ds; ddlDepartment.DataTextField = "DeptUserName" + "-" + "Dept_code"; ddlDepartment.DataValueField = Dept_code ddlDepartment.DataBind(); ddlDepartment.Items.Insert(0, new ListItem("---- Select Department ----", "0")); //con.Close(); }
use below code
private void FillDepartments() { SqlCommand cmdFill = new SqlCommand("SELECT [DeptUserName] + '-' + [Dept_code],Dept_Code as Creator from Depts Order By Dept_code", con); SqlDataAdapter daFill = new SqlDataAdapter(cmdFill); DataSet ds = new DataSet(); daFill.Fill(ds); ddlDepartment.DataSource = ds; ddlDepartment.DataTextField = "Creator"; ddlDepartment.DataValueField = "Dept_code"; ddlDepartment.DataBind(); ddlDepartment.Items.Insert(0, new ListItem("---- Select Department ----", "0")); }
Use this Code to fill
DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(GetConnectionString()); try { connection.Open(); string sqlStatement = "SELECT DeptUserName + ' - ' + Dept_code AS Creator FROM Depts "; SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { DropDownList1.DataSource = dt; DropDownList1.DataTextField = "DeptUserName "; DropDownList1.DataValueField = "Dept_code"; DropDownList1.DataBind(); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } }
这篇关于如何在Dropdownlist asp.net c#sqlerver中连接两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!