2009-10-15 97 views
-1
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"); 
} 
+1

你不妨重新格式化这个并添加一些上下文。 – seanyboy 2009-10-15 07:50:14

+5

你应该阅读一些关于sql注入http://en.wikipedia.org/wiki/SQL_injection – empi 2009-10-15 07:53:48

+3

你在更新声明中使用了错误的密码。我认为正确的密码是“Adama6DaY”。但是你在默默地捕捉并抛开开放的异常,所以你永远不会知道。 – 2009-10-15 07:55:07

回答

0

这可能是一些简单的没有在更新面板与新的数据刷新了控制,但没有更多的信息/背景下,是不可能告诉

0

我想你需要到txthidden.text分配值前检查Request.QueryString["YKcode"];喜欢:

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