2013-08-23 53 views
-2
protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      DropDownList1.DataSource = (DataTable)Session["dt"]; 
      DropDownList1.DataValueField = "base"; 
      DropDownList1.DataTextField = "base"; 
      DropDownList1.DataBind(); 
     } 
    } 

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 


    } 

    string str; 
    string email; 
    string base1; 

    protected void Submit_Click(object sender, EventArgs e) 
    { 
     if (CheckBox9.Checked == true) 
     { 
      str = str + CheckBox9.Text + 'x'; 
     } 


    SqlConnection con = new SqlConnection(...); 
    String sql = "UPDATE INQUIRY2 set Question1 = @str WHERE email = @email AND base = @base;"; 

    SqlCommand cmd = new SqlCommand(sql, con); 
    con.Open(); 

    DataTable theDataTable = null; 

     // Verify that dt is actually in session before trying to get it 

     if(Session["dt"] != null) 
     { 
      theDataTable = Session["dt"] as DataTable; 
     } 



    //Verify that the data table is not null 
    if(theDataTable != null) 
    { 
     email = theDataTable.Rows[0]["email"].ToString(); 
     base1 = theDataTable.Rows[0]["base"].ToString(); 
    } 

    cmd.Parameters.AddWithValue("@email", email); 
    cmd.Parameters.AddWithValue("@str", str); 
    cmd.Parameters.AddWithValue("@base", base1); 

    con.Close(); 

    } 

} 

}提交按钮,复选框不更新SQL Server数据库

+0

你忘了问一个问题。 – tnw

回答

0

你错过了实际的命令执行的呼叫:

cmd.Parameters.AddWithValue("@email", email); 
cmd.Parameters.AddWithValue("@str", str); 
cmd.Parameters.AddWithValue("@base", base1); 

cmd.ExecuteNonQuery(); // <--- this line here 

con.Close(); 

我也强烈建议你包在一个using的连接所以你不会不小心把它打开:

using(SqlConnection con = new SqlConnection(...)) 
{ 
    con.Open(); 

    /* 
     rest of code here 
    */ 

    con.Close(); 
} 
+0

这就是我错过了cmd.Exec ...行!谢谢!!! – user2708783

+0

没问题,很高兴我能帮到你。 – valverij

+0

给你另外一个问题。我在这个页面上有多个复选框。我如何设置一个更新语句,根据问题编号更改“问题”列? – user2708783

相关问题