Click here to Skip to main content
15,908,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//i am getting Exception at line da.Fill(dt); i am developing online Exam

public partial class WebForm9 : System.Web.UI.Page
    {
       
        SqlCommand cmd = new SqlCommand();
        SqlCommand cmd1 = new SqlCommand();
       DataTable dt = new DataTable();
        DataRow drr;
        public string ans_tea;
        public string ans_stu;
        SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=swatidb;Integrated Security=True;Pooling=False");

        static int i = 0;
        
       
       
           public void Page_Load(object sender, EventArgs e)
        {
       
            con.Open();
            string s1 = "select student_id from Student where user_name='" + Session["sid"] + "'";
            SqlCommand cmd = new SqlCommand(s1, con);

            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Label4.Text = dr[0].ToString();
                dr.Close();
            }
            String tea_id = Convert.ToString(TextBox2.Text);
            string commandText = "SELECT * from Question WHERE Teacher_id=@tea_id";
            using (SqlConnection connection = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=swatidb;Integrated Security=True;Pooling=False"))
            {
                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);
                    drr = dt.Rows[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

1 solution

As per my updated solution on your post getting error as Must declare the scalar variable "@tea_id"[^]

The problem is with
C#
String tea_id = Convert.ToString(TextBox2.Text);

Firstly TextBox2.Text is already a String, so no need to convert it.

Secondly it would appear that Teacher_id is probably an integer or a long. Because you have declared tea_id as a string, the line
C#
command.Parameters.AddWithValue("@tea_id", tea_id);
will result in a SQL command of
SQL
SELECT * from Question WHERE Teacher_id='9999'
(where 9999 is whatever was in TextBox2). Note the single quotes around '9999' - i.e. a varchar as far as SQL is concerned.

If you make the correction I have suggested then you will get a SQL command of
SELECT * from Question WHERE Teacher_id=9999

i.e.
C#
long tea_id=long.Parse(TextBox2.Text);


Note: the TryParse method is usually better when accepting input from a user, I've only used Parse for brevity here.
 
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