datalist数据更新问题
错误:未将对象引用到实力
原代码:
public partial class Default2 : System.Web.UI.Page
{
string b;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int a = Convert.ToInt32(Request.QueryString["id"]);
SqlConnection conn;
SqlCommand cmd;
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolhoomConnectionString"].ConnectionString);
string cmdString = @"SELECT * FROM [TeacherMessage] where id=@a";
cmd = new SqlCommand(cmdString, conn);
cmd.Parameters.Add("@a", SqlDbType.VarChar);
cmd.Parameters["@a"].Value = a;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
conn.Close();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "gwc")
{
if (((Button)e.Item.FindControl("gwc")).Text == "返回") //注:gwc为commandName的值,更新返回2个按钮
//的commandName值都为gwc
{
Response.Redirect("Deafault.aspx");
}
if (((Button)e.Item.FindControl("gwc")).Text == "更新")
{
b = ((TextBox)e.Item.FindControl("TextBox1")).Text;
int a = Convert.ToInt32(Request.QueryString["id"]);
SqlConnection conn;
SqlCommand cmd;
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolhoomConnectionString"].ConnectionString);
string cmdString = @"update TeacherMessage set 姓名=@xingming where id=@a";
cmd = new SqlCommand(cmdString, conn);
cmd.Parameters.Add("@a", SqlDbType.VarChar);
cmd.Parameters["@a"].Value = a;
cmd.Parameters.Add("@xingming", SqlDbType.VarChar);
cmd.Parameters["@xingming"].Value = b;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
}
[解决办法]
楼主下次问 最好写个 错误行, 看别人代码其实挺累的,解决了就好。
[解决办法]