更改TextBoxes和Datareader时出现问题 [英] Problem with changing TextBoxes and Datareader

查看:60
本文介绍了更改TextBoxes和Datareader时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友

我正在尝试这样做:

Dear My Friends
I am trying to do this:

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:

 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();    
        }
    }
}





但是通过单击Button1,SQL中的标题不会我发现问题出在datareader上,它将TextBox的值赋予标题。

有没有办法关闭datareader以使用buttonclick更改文本框的值?

祝福



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

推荐答案

问题很可能是 @id 参数值。



Page_Load 方法中,使用querystring参数中的值 Code 。在 Button1_Click 方法中,使用 ViewState 中的值。在发布的代码中,您没有初始化 ViewState 值。



最简单的选择是使用两种方法中的查询字符串:

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:
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();
    }
}


这篇关于更改TextBoxes和Datareader时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆