Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my code:

C#
string strQuery = "Select * from tblStudent where ID = " + txtID.Text;
                da = new SqlDataAdapter(strQuery, con);
                DataSet ds = new DataSet();
                da.Fill(ds,"Students");


Error Message:
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.

Source Error:
Line 25: da = new SqlDataAdapter(strQuery, con);
Line 26: DataSet ds = new DataSet();
Line 27: da.Fill(ds,"Students");
Line 28:
Line 29: ViewState["Sql_Query"] = strQuery;
Posted
Updated 29-Apr-15 18:59pm
v2
Comments
Abhinav S 29-Apr-15 23:00pm    
Is the value of txtID empty or null?
Is ID of a different type than a number?

There are 2 things to consider-
1. If ID is number type then make sure that txtID has some value or not and also make sure that you are passsing numeric data to it.
2. If ID is of type VARCHAR,NVARCHAR,CHAR or NCHAR then you are missing a pair of single quote around the value. Change like this
C#
string strQuery = "Select * from tblStudent where ID = '" + txtID.Text +"'";

Hope, it helps :)
 
Share this answer
 
Comments
[no name] 30-Apr-15 11:36am    
ID is int, primary key and identity. i am trying to get ID as an input from user in the txtID box control to generate rest of the records on the other controls respectively. thank you for the response.
Make sure that the txtID.Text is not empty

try something like this, if the ID is integer field

C#
int id =0;
 int.TryParse(txtID.Text, out id);
 string strQuery = "Select * from tblStudent where ID = " + id;


Note:

be aware of SQL Injection[^] ,Sql Injection Attacks[^]
 
Share this answer
 
Comments
[no name] 30-Apr-15 11:37am    
ID is int, primary key and identity. i am trying to get ID as an input from user in the txtID box control to generate rest of the records on the other controls respectively. thank you for the response.
Karthik_Mahalingam 30-Apr-15 11:43am    
so, still you are getting the same error ?
[no name] 30-Apr-15 14:35pm    
yes
Karthik_Mahalingam 30-Apr-15 14:39pm    
are you online now in gmail ?
Karthik_Mahalingam 30-Apr-15 14:49pm    
deleted

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