用代码更新的问题(在线等。)
我想用代码实现更新数据库内的资料。写了如下代码,但是总是更新不了数据库里的信息,哪位大哥大姐来帮忙分析下。
protected void Page_Load(object sender, EventArgs e)
{
if (Session[ "Admin "] == null) //若尚未登录则转到登录页
{
Response.Redirect( "../index.aspx ");
}
if (!IsPostBack) //如果不是通过返回打开网页,而是第一次打开该网页
{
Label1.Text = Session[ "Admin "].ToString();
}
string strconn = ConfigurationSettings.AppSettings[ "dsn "];
SqlConnection cn = new SqlConnection(strconn);
cn.Open();
string mysql = "select * from login where Username= ' " + Session[ "Admin "].ToString() + " ' ";
SqlCommand cm = new SqlCommand(mysql, cn);
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = dr[ "Name "].ToString();
DropDownList1.Text = dr[ "Sex "].ToString();
TextBox3.Text = dr[ "Telephone "].ToString();
TextBox4.Text = dr[ "Email "].ToString();
TextBox5.Text = dr[ "Adress "].ToString();
dr.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string strconn = ConfigurationSettings.AppSettings[ "dsn "];
SqlConnection cn = new SqlConnection(strconn);
cn.Open();
string mysql1 = "select * from login where Username= ' " + Label1.Text + " ' ";
SqlCommand cm = new SqlCommand(mysql1, cn);
SqlDataReader dr1 = cm.ExecuteReader();
string str1 = (TextBox1.Text).ToLower();
string str2 = (DropDownList1.Text).ToLower();
string str3 = (TextBox3.Text).ToLower();
string str4 = (TextBox4.Text).ToLower();
string str5 = (TextBox5.Text).ToLower();
if (dr1.Read())
{
//dr1.Close();
/*string mysql2 = "update login set Name= ' " + TextBox1.Text.ToString() + " ',Sex= ' " + DropDownList1.Text + " ',Telephone= ' " + TextBox3.Text + " ',Email= ' " + TextBox4.Text + " ',Adress= ' " + TextBox5.Text + " 'where Username= ' " + Session[ "Admin "].ToString() + " ' ";
SqlCommand cm1 = new SqlCommand(mysql2, cn);
cm1.ExecuteNonQuery();
*/
SqlConnection cn1 = new SqlConnection(strconn);
cn1.Open();
SqlCommand cm1 = new SqlCommand( "genxinziliao ", cn1);
cm1.CommandType = CommandType.StoredProcedure;
cm1.Parameters.Add( "@Username ", SqlDbType.NVarChar);
cm1.Parameters.Add( "@Name ", SqlDbType.NVarChar);
cm1.Parameters.Add( "@Sex ", SqlDbType.NVarChar);
cm1.Parameters.Add( "@Telephone ", SqlDbType.NVarChar);
cm1.Parameters.Add( "@Email ", SqlDbType.NVarChar);
cm1.Parameters.Add( "@Adress ", SqlDbType.NVarChar);
cm1.Parameters[ "@Username "].Value = Label1.Text.ToString();
cm1.Parameters[ "@Name "].Value = str1;
cm1.Parameters[ "@Sex "].Value = str2;
cm1.Parameters[ "@Telephone "].Value = str3;
cm1.Parameters[ "@Email "].Value = str4;
cm1.Parameters[ "@Adress "].Value = str5;
cm1.ExecuteNonQuery();
cn1.Close();
Response.Write( " <script> alert( '更新成功! '); </script> ");
}
else
Response.Write( " <script> alert( '更新出错 '); </script> ");
cn.Close();
}
}
[解决办法]
设断点看一下
[解决办法]
加个try捕获异常信息!!!!!
[解决办法]
protected void Page_Load(object sender, EventArgs e)
{
if (Session[ "Admin "] == null) //若尚未登录则转到登录页
{
Response.Redirect( "../index.aspx ");
}
if (!IsPostBack) //如果不是通过返回打开网页,而是第一次打开该网页
{
Label1.Text = Session[ "Admin "].ToString();
string strconn = ConfigurationSettings.AppSettings[ "dsn "];
SqlConnection cn = new SqlConnection(strconn);
cn.Open();
string mysql = "select * from login where Username= ' " + Session[ "Admin "].ToString() + " ' ";
SqlCommand cm = new SqlCommand(mysql, cn);
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = dr[ "Name "].ToString();
DropDownList1.Text = dr[ "Sex "].ToString();
TextBox3.Text = dr[ "Telephone "].ToString();
TextBox4.Text = dr[ "Email "].ToString();
TextBox5.Text = dr[ "Adress "].ToString();
dr.Close();
}
}
}
[解决办法]
SqlCommand cm1 = new SqlCommand( "update login set Name= ' " + TextBox1.Text.ToString() + " ',Sex= ' " + DropDownList1.Text + " ',Telephone= ' " + TextBox3.Text + " ',Email= ' " + TextBox4.Text + " ',Adress= ' " + TextBox5.Text + " 'where Username= ' " + Session[ "Admin "].ToString() + " ' ", cn1);
[解决办法]
你的存储过程有问题吗?