Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I try to create a web site project, but i get "Must declare the scalar variable" error when i build the project. My code is:
C#
SqlConnection conn = new SqlConnection();
                    SqlDataReader dr , dr2;
                    DataTable dt = new DataTable();
                    DataTable dt2 = new DataTable();
                    conn.ConnectionString = "server= **; user = **; pwd=***;
database = ***";
                    conn.Open();
                    string TCNumber = Request.QueryString["TC"].ToString();
                    int intTNumber = Convert.ToInt32(TCNumber);
                    SqlCommand comm = new SqlCommand("SELECT Sistolic,Diastolic,Pulse,Date from MeasureRecords WHERE TC= @intTCNumber;", conn);
                    
                    comm.Connection=conn;
               
                    dr = comm.ExecuteReader();
                 
                    dt.Load(dr);
                    MeasureRecord.DataSource = dt;
                    MeasureRecord.DataBind();

                    SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC=@intTCNumber;", conn);
         
                    comm2.Connection = conn;
                    dr2 = comm2.ExecuteReader();
                
                
                    TCNo.Text = dr2[0].ToString();
                    Name.Text = dr2[1].ToString();
                    Surname.Text = dr2[2].ToString();
                    BirthDate.Text = dr2[3].ToString();
                    Phone_Number.Text = dr2[4].ToString();
                    
                    

                    dr.Close();
                    dr2.Close();
                    conn.Close();


Can you help me? Why this error appears?
Posted
Updated 31-Jul-12 1:28am
v2
Comments
[no name] 31-Jul-12 7:33am    
Change to parameterized queries to help protect yourself from SQL injection attacks.

You have added @intTCNumber in query but it is not you have not declared it.
Add this before executereader.
SqlCommand comm = new SqlCommand("SELECT Sistolic,Diastolic,Pulse,Date from MeasureRecords WHERE TC= @TCNumber;", conn);
comm.Parameters.Add(new SqlParameter("TCNumber", Request.QueryString["TC"].ToString()));
 
Share this answer
 
v4
Comments
sesenyg 31-Jul-12 8:06am    
Thank you so much dude:)
pradiprenushe 31-Jul-12 8:08am    
Welcome.
Your syntax is incorrect.Try this in command

C#
SqlCommand comm = new SqlCommand("SELECT Sistolic,Diastolic,Pulse,Date from MeasureRecords WHERE TC= "+intTCNumber, conn);

SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC= "+intTCNumber, conn);
 
Share this answer
 
v3
Comments
sesenyg 31-Jul-12 7:38am    
Thank you for your solution it helped me but now it says "the conversion of the varchar value '4577765884' overflowed an int column." Do you know what is this?
Santhosh Kumar Jayaraman 31-Jul-12 7:39am    
int intTNumber = Convert.ToInt32(TCNumber);

in this line instead of int, make it as int64
int64 intTNumber = Convert.ToInt64(TCNumber);
sesenyg 31-Jul-12 7:41am    
No, it did not work. It said the same thing again:(
Santhosh Kumar Jayaraman 31-Jul-12 7:45am    
int64 intTNumber = Convert.ToInt64(TCNumber);

Have you replaced it in both ends? and at both lines in your code? In which line you are getting error?
sesenyg 31-Jul-12 7:50am    
Actually, my parameter TC is string which i used in sql command. Firstly, i try to do
string TCNumber = Request.QueryString["TC"].ToString();
SqlCommand comm = new SqlCommand("SELECT Sistolic,Diastolic,Pulse,Date from MeasureRecords WHERE TC= @TCNumber;", conn);

but it gave me errors, so i change the data type. I think the first problem is my data TC on database is a string but i try to match it with an int.
SqlConnection conn = new SqlConnection();
                    SqlDataReader dr , dr2;
                    DataTable dt = new DataTable();
                    DataTable dt2 = new DataTable();
                    conn.ConnectionString = "server= **; user = **; pwd=***;
database = ***";
                    conn.Open();
                    string TCNumber = Request.QueryString["TC"].ToString();
                    //int intTNumber = Convert.ToInt32(TCNumber);
                   SqlCommand comm = new SqlCommand("SELECT Sistolic,Diastolic,Pulse,Date from MeasureRecords WHERE TC= '"+TCNumber+ "'", conn);
                    
                    comm.Connection=conn;
               
                    dr = comm.ExecuteReader();
                 
                    dt.Load(dr);
                    MeasureRecord.DataSource = dt;
                    MeasureRecord.DataBind();
 
                    SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC='"+TCNumber+"'", conn);
         
                    comm2.Connection = conn;
                    dr2 = comm2.ExecuteReader();
                
                
                    TCNo.Text = dr2[0].ToString();
                    Name.Text = dr2[1].ToString();
                    Surname.Text = dr2[2].ToString();
                    BirthDate.Text = dr2[3].ToString();
                    Phone_Number.Text = dr2[4].ToString();
                    
                    
 
                    dr.Close();
                    dr2.Close();
                    conn.Close();
 
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