Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//table is Question and colums are question_id,teacher_id,teacher_question,teacher_answer
//table is Answer and colums are Answer_id,Question_id,student_id,student_answer
//here is code

C#
int qu_i = Convert.ToInt32(TextBox1.Text);
            cmd1 = new SqlCommand("select ans from Que1 where que_id=@qu_i",con);
          string s1 = Convert.ToString(cmd1);
          Label4.Text = Convert.ToString(cmd1);

            //Decimal sa = Convert.ToDecimal(cmd1);
         // Response.Write(s1);
           int qu_id = Convert.ToInt32(TextBox1.Text);
           cmd2 = new SqlCommand("select ans123 from Ans1 where que_id=@qu_i", con);
           Label5.Text = Convert.ToString(cmd2);
           string s2 = Convert.ToString(cmd2);

           Response.Write(s2);
          // Decimal sb = Convert.ToDecimal(cmd2);
          // int j = cmd2.ExecuteNonQuery();
          // s1 = Convert.ToString(i);
            //s2 = Convert.ToString(j);
               MatchsMaker match = new MatchsMaker(s1,s2);// this line is for comparing two strings
               b = match.Score;
               Response.Write(b);
           // Response.Write(match.Score);
            Label3.Text = Convert.ToString(match.Score);
Posted
Updated 19-Mar-14 19:24pm
v2

1 solution

Creating an SqlCommand object does not fetch anything from a database - you have to specifically instruct it to execute the command - which your code doesn't do. In addition, converting an SqlCommand object to a string will just return "System.Data.SqlClient.SqlCommand" rather than any data it might be associated with. And it would help if you passed your parameterized value over as well...

Try this:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT ans FROM Que1 WHERE que_id=@qu_i", con))
        {
        cmd.Parameters.AddWithValue("@qu_i", qu_i);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                string ans = (string) reader["ans"];
                Console.WriteLine("Answer is: {0}", ans);
                }
            }
        }
    }
 
Share this answer
 
Comments
swati gapat 19-Mar-14 23:54pm    
thanks sir it is working
OriginalGriff 20-Mar-14 5:14am    
You're welcome!

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