Click here to Skip to main content
15,886,007 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am contacting 3 table field using
CONCAT

they are showing single filed when running query in sql server management studio.

but they are showing different columns when using in C#


I want "FirstName + ,MiddleName + LastName, EmailAddress" in single field so that
i can assign it to DataGridViewComboBoxCell.displaymember

What I have tried:

C#
<pre>
String query3 = "Select CONCAT(CONCAT (FirstName ,' ',MiddleName , ' ' , LastName), ',',EmailAddress ) As Contacts ";

Query2 = query3 + "From Contacts ";
Query2 = Query2 + "WHERE  CompanyID=";
Query2 = Query2 + CompanyID;
Query2 = Query2 + "And (IsDelete Is null Or IsDelete = 0 )";


Here final query be like
/*
Select CONCAT(CONCAT (FirstName ,' ',MiddleName , ' ' , LastName), ',',EmailAddress ) As Contact
From Contacts WHERE  CompanyID=4000 And (IsDelete Is null Or IsDelete = 0 )"
*/

DataSet ContactDS = new DataSet();
ContactDS = StMethod.GetListDS<ManagerSetColumn2>(Query2);
               
DataGridViewComboBoxCell ContactCheckCmb = new DataGridViewComboBoxCell();
ContactCheckCmb.DataSource = ContactDS.Tables[0];


// here it is showing all filed as seprate one like FirstName,LastName,MiddleName,EmailAddress,Contacts

 foreach (DataColumn column in ContactDS.Tables[0].Columns)
{
	MessageBox.Show(column.ColumnName);
}


I want "FirstName + ,MiddleName +  LastName, EmailAddress" in single field so that
i can assign it to DataGridViewComboBoxCell.displaymember
Posted
Updated 2-Sep-21 21:10pm

Since the query returns correct results and seems fine, the problem is most likely in method GetListDS. Using the debugger, go through the code, does it for example split a column if comma is found in the data...

Another observation, why do you use two separate concat functions, wouldn't only one suffice?

Third and perhaps the most important observation, never concatenate values directly to SQL statement. This leaves you open to SQL injection and other problems. Instead, use parameters. For examples, have a look at Properly executing database operations[^]
 
Share this answer
 
Comments
Devendra Sarang 3-Sep-21 1:32am    
ok. checking GetListDS.

till then how i can prevent my data from sql injection regarding this case.
Wendelius 3-Sep-21 1:55am    
Use parameters, please have a look at the article I linked.
Your code is SQL Injection[^] vulnerable.

Never use concatenated string! Use parameterized queries[^] instead.

For example:
C#
string sConStr = @"your_connection_string_here!";
DataTable dt = new DataTable();

//1. create SqlConnection
using(SqlConnection connection  = new SqlConnection(sConStr))
{
    //open connection
    connection.Open()
    //set command text
    string sql = @"SELECT * FROM TableName WHERE TextField Like @SomeString;";
    //2. create command
    using (SqlCommand command = new SqlCommand(sql, connection))
    {
        command.Parameters.AddWithValue("@SomeString", "%whatever%")
        //3. create reader
        using (SqlDataReader reader = command.ExecuteReader())
        {
            //4. load data into datatable
            dt.Load(reader)
        }
    }
}


For further details, please read this:
Data Security: Stop SQL Injection Attacks Before They Stop You | Microsoft Docs[^]

So, your final query should looks like:
C#
string query3 = @"Select CONCAT (FirstName, ' ', MiddleName, ' ', LastName, ', ', EmailAddress ) As ContactDetails 
From Contacts 
WHERE  CompanyID=@CompanyID And (IsDelete Is null Or IsDelete = 0)";

Then you have to pass @CompanyID parameter ;)


Good luck!
 
Share this answer
 

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