Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my Question table colums are Question_id,tea_question,teacher_id,tea_answer.
i want to display only selected teacher_id's Questions on a label so how can i solve it
I am writing query for online Examination so each time student click on next button only only that teacher_id's Question should be display on label.
i tried following code but it gives me an Exception as "Must declare the scalar variable "@tea_id"."

//code is given below

public partial class WebForm9 : System.Web.UI.Page
    {
       
        SqlCommand cmd = new SqlCommand();
     
       DataTable dt = new DataTable();
        DataRow drr;
     static int i = 0;
  public void Page_Load(object sender, EventArgs e)
        {
           String tea_id = Convert.ToString(TextBox2.Text);
            SqlDataAdapter da = new SqlDataAdapter("select * from Question where Teacher_id=@tea_id ", con);
           da.Fill(dt);
            drr = dt.Rows[i];

            //Label2.Text = "Write question below in Textbox  click next to get questoion";
               
           // i++;
        }
           public void Button2_Click(object sender, System.EventArgs e)
        {
            
          //  Label2.Text = Convert.ToString(dr[2]);
           
            //Label3.Text = Convert.ToString(dr[3]);
            //TextBox3.Text = Convert.ToString(dr[2]);
            //TextBox4.Text = Convert.ToString(dr[3]);
                                 if (i == (dt.Rows.Count - 1))
            {
                Response.Write("Last record !");
            }
            else
            {
                //i = 0;
               i++;
             
            }
                                // drr = dt.Rows[i];
                                 Label3.Text = Convert.ToString(drr[0]);
                                 Label2.Text = Convert.ToString(drr[2]);
 
                      //Label2.Text = Convert.ToString(dr[2]);
                                // i = 0;
                                 con.Close();
       }
}
}
Posted

In the line
SQL
SqlDataAdapter da = new SqlDataAdapter("select * from Question where Teacher_id=@tea_id ", con);

you have declared the variable @tea_id but you have not provided the parameter to the command
e.g.
cmd.Parameters.Add(new SqlParameter("tea_id", tea_id));


There is more wrong than just this so have a look here for a tutorial[^] on using parameterised queries

[EDIT] In response to OP comment...
Instead of String tea_id = Convert.ToString(TextBox2.Text); (by the way Convert.ToString was unnecessary TextBox2.Text is already a string) you probably want something like long tea_id = long.Parse(this.textBox1.Text);
Because tea_id was declared as string the parameter will have surrounded it by single quotes when inserting the variable into the sql string
 
Share this answer
 
v2
Comments
swati gapat 22-Mar-14 12:53pm    
now getting error as
Error converting data type nvarchar to numeric.at line da.Fill(dt);
CHill60 22-Mar-14 13:32pm    
I've updated my solution with a fix for that
As the error itself says something,
Quote:
"Must declare the scalar variable "@tea_id"
. You need to declare that parameter for the given command.

I have changed little bit of your code, see below
C#
int tea_id = int.Parse(TextBox2.Text);
SqlCommand cmd = new SqlCommand("select * from Question where Teacher_id=@tea_id", con);
cmd.Parameters.AddWithValue("@tea_id", tea_id);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
// rest of your code...


-KR
 
Share this answer
 
v2
Comments
swati gapat 22-Mar-14 12:53pm    
now getting error as
Error converting data type nvarchar to numeric.at line da.Fill(dt);
Krunal Rohit 22-Mar-14 13:56pm    
int id = int.Parse(TextBox2.Text);
// rest of the code is same...

-KR
change this
 SqlDataAdapter da = new SqlDataAdapter("select * from Question where Teacher_id=@tea_id ", con);
da.Fill(dt);

to
string commandText = "SELECT * from Question WHERE Teacher_id=@tea_id";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand(commandText, con))
    {
        command.Parameters.AddWithValue("@tea_id", tea_id);
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        da.Fill(dt);
        // your other code...
    }
}
 
Share this answer
 
v2
Comments
swati gapat 22-Mar-14 12:53pm    
now getting error as
Error converting data type nvarchar to numeric.at line da.Fill(dt);
Peter Leow 22-Mar-14 13:01pm    
What is the data type of tea_id?
swati gapat 22-Mar-14 14:12pm    
it is numeric
Hi,
when ever you pass any parameter/variable to the SQL, please consider the data type of the column for which you are passing this. SO it is always suggested that parameter with datatype mentioned.

SQL
cmd.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12).Value = "Sample Value";


Doing this will avoid your chances of getting error at the receiving end i.e. Sql Server
 
Share this answer
 
You have to add command parameter like that.. :)


C#
command.Parameters.AddWithValue("@tea_id", tea_id);


So the final code is...

C#
int tea_id = int.Parse(TextBox2.Text);
SqlCommand cmd = new SqlCommand("select * from Question where Teacher_id=@tea_id", con);
cmd.Parameters.AddWithValue("@tea_id", tea_id);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
//Remaining your code...
 
Share this answer
 
v2

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