Click here to Skip to main content
15,885,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my Question table colums are que_id int,que varchar,ans varchar
i am using following code which is executing but values are not updating in table.

//code is
SqlCommand cmd1 = new SqlCommand();
       SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=DB;Integrated Security=True;Pooling=False");
        protected void Page_Load(object sender, EventArgs e) 
        {
            cmd1.Connection = con;
            con.Open();
           string tea_que, tea_ans;
            using (cmd1 = new SqlCommand("SELECT que , ans FROM Que1  WHERE que_id =27", con))
            {
                 using (SqlDataReader reader = cmd1.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        tea_que = (string)reader["que"];//for fetching values from table
                        tea_ans = (string)reader["ans"];
                        TextBox1.Text = tea_que;
                        TextBox2.Text = tea_ans;
                      }
                }
             //con.Close();
               }
            }
        
            protected void update_Click(object sender, EventArgs e)
             {
            try
            {
                cmd1 = new SqlCommand("UPDATE Que1 SET que='" + TextBox1.Text + "',ans='" + TextBox2.Text + "' WHERE que_id='" + Label5.Text + "'", con);
                cmd1.Parameters.Add("@que_id12",SqlDbType.Int).Value= Label5.Text;
                cmd1.Parameters.Add("@que",SqlDbType.VarChar).Value= TextBox1.Text;
                cmd1.Parameters.Add("@que_ans",SqlDbType.VarChar).Value= TextBox2.Text;
               // con.Open();
                int i = cmd1.ExecuteNonQuery();
                con.Close();
                    Response.Write("changed");
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                    }
            catch (Exception e1)
            {
                Label3.Text = "not updated successfully"+e1;
            }
        }
Posted
Updated 27-Mar-14 20:00pm
v2
Comments
King Fisher 28-Mar-14 2:24am    
getting error?
swati gapat 28-Mar-14 3:05am    
no sir not getting error , values are not updating in table
Bernhard Hiller 28-Mar-14 5:49am    
int i = cmd1.ExecuteNonQuery();
and i is 0, isn't it?

Try by changing this line
C#
cmd1 = new SqlCommand("UPDATE Que1 SET que='" + TextBox1.Text + "',ans='" + TextBox2.Text + "' WHERE que_id='" + Label5.Text + "'", con);

to

C#
cmd1 = new SqlCommand("UPDATE Que1 SET que=@que,ans=@que_ans WHERE que_id=@que_id12", con);
 
Share this answer
 
Comments
swati gapat 28-Mar-14 3:23am    
sir i have tried this solution, command is executing but table is not updating
ArunRajendra 28-Mar-14 3:28am    
Can you post your modified code?
yeah the reason you r getting error is
1. you r not opening the connection
2. if you r opening the connection then Why u r storing cmd1.ExecuteNonQuery() in int i ?

the you r executing get stores in variable i and you are not executing it.

Simply remove int i just write




SqlCommand cmd1 = new SqlCommand();
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=DB;Integrated Security=True;Pooling=False");
protected void Page_Load(object sender, EventArgs e)
{
cmd1.Connection = con;
con.Open();
string tea_que, tea_ans;
using (cmd1 = new SqlCommand("SELECT que , ans FROM Que1 WHERE que_id =27", con))
{
using (SqlDataReader reader = cmd1.ExecuteReader())
{
while (reader.Read())
{
tea_que = (string)reader["que"];//for fetching values from table
tea_ans = (string)reader["ans"];
TextBox1.Text = tea_que;
TextBox2.Text = tea_ans;
}
}
//con.Close();
}
}

protected void update_Click(object sender, EventArgs e)
{
try
{
cmd1 = new SqlCommand("UPDATE Que1 SET que='" + TextBox1.Text + "',ans='" + TextBox2.Text + "' WHERE que_id='" + Label5.Text + "'", con);
cmd1.Parameters.Add("@que_id12",SqlDbType.Int).Value= Label5.Text;
cmd1.Parameters.Add("@que",SqlDbType.VarChar).Value= TextBox1.Text;
cmd1.Parameters.Add("@que_ans",SqlDbType.VarChar).Value= TextBox2.Text;
con.Open();
/*********** Dont store value in int i if you ll do it will not execute your code.***************
cmd1.ExecuteNonQuery();
con.Close();
Response.Write("changed");
TextBox1.Text = "";
TextBox2.Text = "";
}
catch (Exception e1)
{
Label3.Text = "not updated successfully"+e1;
}
}
 
Share this answer
 
Comments
[no name] 28-Mar-14 14:42pm    
1. Possibly. The connection is being opened in Page_Load.
2. Nothing wrong with that at all since i would contain the number of rows affected by the query. So your comment is ridiculous.
The bigger question is why he, and you, are using string concatenation to construct the query. And why are you using command parameters for the same thing and throwing them away.
Tarun Jaiswal 30-Mar-14 2:35am    
read my earlier post i have mention that if connection is opened do you understand,
there is logical error in code don't you see ?

the error part is this -----> int i = cmd1.ExecuteNonQuery();
she only have write this much of code -----> cmd1.ExecuteNonQuery();
if she store function in int i it will never execute it and there will be no affect in database.
do you get it ?


[no name] 28-Apr-14 16:25pm    
No that is not an error at all. If you knew what functions are and what functions return, you would see that it's not an error at all. That has nothing at all do do with anything.
SQL
cmd1 = new SqlCommand("UPDATE Que1 SET que='" + TextBox1.Text + "',ans='" + TextBox2.Text + "' WHERE que_id='" + Label5.Text + "'", con);
                cmd1.Parameters.Add("@que_id12",SqlDbType.Int).Value= Label5.Text;
                cmd1.Parameters.Add("@que",SqlDbType.VarChar).Value= TextBox1.Text;
                cmd1.Parameters.Add("@que_ans",SqlDbType.VarChar).Value= TextBox2.Text;



underlined que-id is int "+ label.text+"
 
Share this answer
 
First of all don't name your variables like conn1, cmd2, Label8, it's bad practice.
Many articles, chapters and so on have been written about it.

Also if there is no special reason for using single SqlCommand for entire class.

Convert string values to numeric, because:
C#
object os = "5";
object on = 5;
are not same thing.

Use try/catch/finally block and close connection there. If necessary read about it.

For your basic problem try this:
C#
protected void update_Click(object sender, EventArgs e)
{
    string updateQuery = "UPDATE Que1 SET que=@que, ans=@que_ans WHERE que_id=@que_id";

    try
    {
        using(SqlCommand cmd = new SqlCommand(updateQuery , con))
        {
             cmd.Parameters.Add("@que_id",SqlDbType.Int).Value = int.Parse(Label5.Text);
             cmd.Parameters.Add("@que",SqlDbType.VarChar).Value = TextBox1.Text;
             cmd.Parameters.Add("@que_ans",SqlDbType.VarChar).Value = TextBox2.Text;
              
             if(con.State != ConnectionState.Open)
             {
                 con.Open();
             }

             cmd.ExecuteNonQuery();
               
             Response.Write("changed");
             TextBox1.Text = string.Empty;
             TextBox2.Text = string.Empty;
         }
     }
     catch (Exception ex)
     {
        //Seriously adopt standard naming convention ;)
        Label3.Text = string.Format("Not updated successfully {0}",ex);
     }
     finally
     {
        if(con.State != ConnectionState.Closed)
        {
            con.Close();
        }
     }
}


I hope this helps.
 
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