Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one DropDown List I want to bind it from Two table i.e id from one table and description in another table
on Filling the data Set giving Sql Exception
The Query For binding Drop Down list is
SQL
SELECT     
CustomerGroupContacts.cusg_id,
CustomerGroupContacts.cusg_id+'___'+CustomerIDs.cusid_name as customername
FROM        
CustomerGroupContacts INNER JOIN
CustomerIDs ON CustomerGroupContacts.cusg_id = CustomerIDs.cusg_id
Posted
Updated 27-Dec-11 18:38pm
v2
Comments
thatraja 28-Dec-11 0:38am    
What's the Sql Exception? Always mention it in your question
waqar ahmad (Dev) 28-Dec-11 0:41am    
System.Data.SqlClient.SqlException: Incorrect syntax near 'CustomerGroupContacts'.

Source Error:


Line 30: {
Line 31: da.SelectCommand.CommandType = CommandType.Text;
Line 32: da.Fill(ds, "CustomerGroupContacts");
Line 33: if (ds.Tables.Count > 0)
Line 34: {
nagendrathecoder 28-Dec-11 0:43am    
What is the exception?
Zubair Alie 28-Dec-11 0:48am    
query should not provide any exception. try this query in query editor of management studio.
focus on the connection string or (DB credentials/Domain Name)
may you have any problem in connection.
Zubair Alie 28-Dec-11 0:51am    
try writing your query in simpler way.. try this

SELECT
a.cusg_id,
(a.cusg_id+'___'+b.cusid_name) as customername
FROM
CustomerGroupContacts a INNER JOIN
CustomerIDs b ON a.cusg_id = b.cusg_id

I think CustomerGroupContacts.cusg_id is integer, convert this to varchar and then append to customer name.

SQL
SELECT     
CustomerGroupContacts.cusg_id,
CAST(CustomerGroupContacts.cusg_id AS varchar(20)) + '___' + CustomerIDs.cusid_name as customername
FROM        
CustomerGroupContacts INNER JOIN
CustomerIDs ON CustomerGroupContacts.cusg_id = CustomerIDs.cusg_id<pre lang="SQL">
 
Share this answer
 
Comments
waqar ahmad (Dev) 28-Dec-11 0:45am    
Customer cusg_id char(4) its not integer
anushripatil 28-Dec-11 0:58am    
is the query returning you the value in SQL query analyzer ?? if yes then plz check ur code.
is the query returning you the value in SQL query analyzer ?? if yes then plz check ur code.U might b missing something.
Pls chk the following sample code
C#
string strConnection = "Server=;Database=Testing;user ID=;password=; ";
           SqlConnection sqlConn = new SqlConnection(strConnection);
           string strQuery = "UR QUERY ";
           sqlConn.Open();
           SqlCommand sqlComm = new SqlCommand(strQuery,sqlConn);
           SqlDataAdapter sqa= new SqlDataAdapter(sqlComm);
           DataSet  ds=new DataSet();
           sqa.Fill(ds);
 
Share this answer
 
v2
Comments
waqar ahmad (Dev) 28-Dec-11 1:09am    
My Code is for Filling Customer DropDownlist is
it giving error on dataset
public void FillCustomer(DropDownList ddsuplier)
{
DataSet ds = new DataSet();
using (SqlConnection con = ConnectionManager.GetConnection())
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT " +
"CustomerGroupContacts.cusg_id, " +
"CustomerGroupContacts.cusg_id+'___'+CustomerIDs.cusid_name as customername" +
"FROM " +
"CustomerGroupContacts INNER JOIN " +
"CustomerIDs ON CustomerGroupContacts.cusg_id = CustomerIDs.cusg_id", con))
{

da.SelectCommand.CommandType = CommandType.Text;

da.Fill(ds, "CustomerGroupContacts");
if (ds.Tables.Count > 0)
{
ddsuplier.DataSource = ds;
ddsuplier.DataTextField = ds.Tables["CustomerGroupContacts"].Columns["customername"].ToString();
ddsuplier.DataValueField = ds.Tables["CustomerGroupContacts"].Columns["CustomerGroupContacts.cusg_id"].ToString();
ddsuplier.DataBind();
}


}
}
}
anushripatil 28-Dec-11 1:13am    
sqlConn.Open(); missing
//u r missing the connection string
ConfigurationManager.ConnectionStrings[connectionName]
anushripatil 28-Dec-11 1:24am    
//u r missing the connection string
ConfigurationManager.ConnectionStrings[connectionName]
waqar ahmad (Dev) 28-Dec-11 2:19am    
I open it but still giving exception
System.Data.SqlClient.SqlException: Incorrect syntax near 'CustomerGroupContacts'.
Actuallay i have two tables
The id is one table1 and description in another table2 and i have to bind ID value dd.DataValueField from table1 and and description dd.DataTextField from Table2
anushripatil 28-Dec-11 2:28am    
u can have id from 1 table & desciption from other table ... the problem is not in the query as far as i feel .... Pls debug & chk if u r getting a valid connection string . Coz i guess its probl;em with connection string
i think dropdown access only one value..
 
Share this answer
 
Comments
Zubair Alie 28-Dec-11 1:32am    
to DropDown you can assign the front end value (TEXT) and back end id (VALUE)
see
ddl.DataTextField
dd.DataValueField

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