更新不起作用

时间:2009-10-15 07:48:45

标签: c# asp.net

protected void Page_Load(object sender, EventArgs e)
{
    txtHidden.Text = Request.QueryString["YKcode"];
    Display();
}

private void Display()
{
    SqlDataReader reader;
    SqlConnection con = new SqlConnection("Data Source=Localhost;Initial Catalog=MLC000022;User ID=sa;Password=Adama6DaY; Integrated Security=True");
    SqlCommand cmd = new SqlCommand("SELECT " + "  dbo.GMYAKU.NAME, " +"FROM " +
                                    "  dbo.GMYAKU " + "   WHERE " +
                                    "  (dbo.GMYAKU.YKCODE = ('" + txtHidden.Text + "'))",con) ;    
    con.Open();
    reader = cmd.ExecuteReader();
    if (reader.Read())
    {
        this.TextBox1.Text = reader["NAME"].ToString();
    }
    else
    {      
        // 読めないので画面を初期化する
     }

    cmd.Connection.Close();
    cmd.Dispose();
    con.Close();
}

protected void Button1_Click(object sender, EventArgs e)
{
    string connetionString = null;
    SqlConnection cnn;
    SqlCommand cmd;

    connetionString = ("Data Source=Localhost;Initial Catalog=MLC000022;User ID=sa;Password=redacted; Integrated Security=True");

    string strSQL ;
    strSQL = "UPDATE GMYAKU SET";
    strSQL += " NAME = '" + (TextBox1.Text) + "'";
    strSQL += " WHERE";
    strSQL += " YKCODE= '" + txtHidden.Text + "'";
    cnn = new SqlConnection(connetionString);
    try
    {
        cnn.Open();
        cmd = new SqlCommand(strSQL, cnn);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        // cnn.Close();
        //MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
    }
    catch (Exception ex)
    {
        // MessageBox.Show("Can not open connection ! ");
    }

    Response.Redirect("Default.aspx");
 }

2 个答案:

答案 0 :(得分:0)

这可能就像在更新面板中没有控件来刷新新数据一样简单,但没有更多信息/上下文就无法分辨

答案 1 :(得分:0)

我认为您需要先检查Request.QueryString["YKcode"];,然后再将值txthidden.text分配给 protected void Page_Load(object sender, EventArgs e) { if(!Request.QueryString["YKcode"].equals("") && Request.QueryString["YKcode"]!=null) { txtHidden.Text = Request.QueryString["YKcode"]; Display(); } }

{{1}}