Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
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.
Posted

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();
}
 
Share this answer
 
use below code

C#
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"));
}
 
Share this answer
 
Use this Code to fill

C#
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();
        }
    }
 
Share this answer
 
v2
Comments
Gihan Liyanage 3-Sep-14 7:00am    
I have edited my code with full try catch. This may help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900