Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my select query

C#
String selQuery = "SELECT (Bus_Route,Cost) FROM MapDataImage WHERE Source='" + Source_Box.Text + "' AND Destination='" + Distance_Box.Text + "'";
            {
                scmd1 = new SqlCommand(selQuery, conn2);
                SqlDataReader sqldread2 = scmd1.ExecuteReader();
                RouteDetails.Text = sqldread2["Bus_Route" + " Cost "].ToString();

            }


Executequery gives me error call

Incorrect syntax near ','.


basically i want to add two column data into one textbox

so is this method wrong?

Thanks
Posted

C#
String selQuery = "SELECT Bus_Route,Cost FROM MapDataImage WHERE Source=@SC AND Destination=@DS";
            
                scmd1 = new SqlCommand(selQuery, conn2);
                scmd1.Parameters.AddWithValue("@SC", Source_Box.Text);
                scmd1.Parameters.AddWithValue("@DS", Distance_Box.Text);
                SqlDataReader sqldread2 = scmd1.ExecuteReader();
                if (sqldread2.Read())
                {
                    string newline = "Bus Route  = " + sqldread2["Bus_Route"].ToString() + "\r\n" + "Cost  = " + sqldread2["Cost"].ToString();
                    RouteDetails.Text = newline;
                    
                }


Done
 
Share this answer
 
v3
Remove the brackets to start with:
C#
String selQuery = "SELECT Bus_Route,Cost FROM MapDataImage WHERE Source='" + Source_Box.Text + "' AND Destination='" + Distance_Box.Text + "'";
Then stop doing SQL like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:
C#
String selQuery = "SELECT Bus_Route,Cost FROM MapDataImage WHERE Source=@SC AND Destination=@DS";
scmd1 = new SqlCommand(selQuery, conn2);
scmd1.Parameters.AddWithValue("@SC", Source_Box.Text);
scmd1.Parameters.AddWithValue("@DS", Distance_Box.Text);
SqlDataReader sqldread2 = scmd1.ExecuteReader();
if (sqldread2.Read())
   {
   RouteDetails.Text = sqldread2["Bus_Route" + " Cost "].ToString();
   }
 
Share this answer
 
Comments
promod madushan 27-Jul-13 3:33am    
it still gives me error in final code...

RouteDetails.Text = sqldread2["Bus_Route" + " Cost "].ToString();

error...
says
Bus_Route Cost
promod madushan 27-Jul-13 3:38am    
if i do like this
RouteDetails.Text = sqldread2["Bus_Route"].ToString();
it will give an answer...
but i want to get two column details into one textbox
OriginalGriff 27-Jul-13 3:55am    
Then you need to either change the query, or merge them in the C# code.
C# is probably easier:
RouteDetails.Text = string.Format("{0} {1}", sqldread2["Bus_Route"], sqldread2[Cost"]);

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