Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear My Friends
I am trying to do this:
C#
protected void Page_Load(object sender, EventArgs e)
      {
          int id = Convert.ToInt32(ViewState["id"]);
          SqlConnection con = new SqlConnection();
          con.ConnectionString = "";
          string q = "select * from ooo where id = @id";
          SqlCommand cmd = new SqlCommand(q, con);
          cmd.Parameters.AddWithValue("@id", Request.QueryString["Code"]);
          Label1.Text = id.ToString();
          con.Open();
          SqlDataReader dr = cmd.ExecuteReader();
          dr.Read();
          TextBox1.Text = dr["title"].ToString();
          con.Close();
          dr.Close();
          dr.Dispose();
      }

And again I tried this:
C#
 protected void Button1_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(ViewState["id"]);
            SqlConnection con = new SqlConnection()
            //con.ConnectionString = "";
            string q = "update ooo set title=@title where id=@id";
            SqlCommand cmd = new SqlCommand(q, con);
            cmd.Parameters.Add("@id", id);
            cmd.Parameters.AddWithValue("@title",TextBox1.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();    
        }
    }
}


But by clicking on the Button1 the title in SQL does not change.I found that the problem is about datareader which gives the value of TextBox as title.
Is there any way to turn off datareader for changing the value of textbox using of buttonclick?
Best wishes
Posted
Updated 29-Jun-15 4:00am
v2

1 solution

The problem is most likely with the @id parameter value.

In your Page_Load method, you use the value from the querystring parameter Code. In your Button1_Click method, you use the value from ViewState. At no point in the posted code have you initialized the ViewState value.

The simplest option would be to use the querystring in both methods:
C#
protected void Page_Load(object sender, EventArgs e)
{
    // Wrap all disposable objects in a "using" block:
    using (var connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
    using (var command = new SqlCommand("SELECT title FROM ooo WHERE id = @id", connection))
    {
        command.Parameters.AddWithValue("@id", Request.QueryString["code"]);
        
        connection.Open();
        
        // No need to use a datareader here, since you're only selecting a single value:
        TextBox1.Text = Convert.ToString(command.ExecuteScalar());
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    using (var connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
    using (var command = new SqlCommand("UPDATE ooo SET title = @title WHERE id = @id", connection))
    {
        command.Parameters.AddWithValue("@title", TextBox1.Text);
        command.Parameters.AddWithValue("@id", Request.QueryString["code"]);
        
        connection.Open();
        command.ExecuteNonQuery();
    }
}
 
Share this answer
 
Comments
Member 11796666 30-Jun-15 2:43am    
Dear Richard
I tried your advise, But the problem has not been solved.
sasanka sekhar panda 30-Jun-15 3:17am    
so now what is the exact problem you are facing..and where u r facing the problem in page load or in button click..
Member 11796666 30-Jun-15 7:05am    
In fact, Datareader does not allow to button click to change the title in sql. textbox is affected by datareader and the its value does not change.(textbox1 is showing the datareader values and also is used to change the title). I want to eliminate the effect of datareader when the button is clicked.
best wishes
sasanka sekhar panda 30-Jun-15 10:18am    
please properly read the solution provided by Rechard..use execute scalar instead of datareader .Why you just want use datareader i wo not understand
Richard Deeming 30-Jun-15 6:45am    
You'll need to provide more information than that.

Remember, we can't see your screen, access your hard-drive, or read your mind! :)

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