从数据库NOT WORKING获取值后,通过文本框更新值
本文关键字:文本 更新 数据库 NOT WORKING 获取 | 更新日期: 2023-09-27 18:25:40
我们从数据库中获取值,并在加载页面时向它们显示文本框,然后在更新时,该页面的点击事件不起作用,并自动重定向到主页面。我们需要更新数据库中获取的值。
public partial class pages_update : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["id"].ToString());
if (!Page.IsPostBack)
{
// txtname.Text = this.txtname.Text;
SqlConnection connection = new SqlConnection(@"Data Source=.'SQLEXPRESS;Initial Catalog=studreg;Integrated Security=True");
DataTable dt = new DataTable();
connection.Open();
SqlDataReader iReader = null;
/*
if (id != 0)
{
if (!this.IsPostBack)
{
txtname.Text = id;
//msg_lbl.Text = "Inside not PostBack";
}
}
else
Response.Write("Invalid URL for article");
*/
string images = Request.QueryString["image1"];
SqlCommand cmd = new SqlCommand("select id,(cast(fname as varchar(10))+''+cast(mname as varchar(10))+''+cast(lname as varchar(10)))as concatenated ,qualification,collage,dob,image,address,contact,email, technology from studreg", connection);
iReader = cmd.ExecuteReader();
int status = 0;
while (iReader.Read())
{
int chkid = int.Parse(iReader["id"].ToString()); ;
if (id == chkid && status == 0)
{
status = 1;
// txtid.Text = (iReader["id"].ToString());
txtname.Text = (iReader["concatenated"].ToString());
txtqualification.Text = (iReader["qualification"].ToString());
txtcollage.Text = (iReader["collage"].ToString());
// txtgender.Text = (iReader["gender"].ToString());
txtdob.Text = (iReader["dob"].ToString());
string path = string.Concat(@"/internship/pages/images/", iReader["image"].ToString());
Image1.ImageUrl = path;
txtaddress.Text = (iReader["address"].ToString());
txtcontact.Text = (iReader["contact"].ToString());
txtemail.Text = (iReader["email"].ToString());
txttechnology.Text = (iReader["technology"].ToString());
// break;
} //end if
} // end while
} // ennd ispostback
} // page load
// connection.Close();
//protected void UPDATE_Click(object sender, EventArgs e)
protected void UPDATE_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(@"Data Source=.'SQLEXPRESS;Initial Catalog=studreg;Integrated Security=True");
SqlCommand cmd = new SqlCommand("update studreg set qualification=@qualification,collage=@collage,dob=@dob,address=@address,contact=@contact,email=@email,technology=@technology where id=@id", connection);
if (connection.State != System.Data.ConnectionState.Open)
{
connection.Open();
// cmd.Parameters.AddWithValue("@concatenated", txtname.Text);
cmd.Parameters.AddWithValue("@qualification", txtqualification.Text);
cmd.Parameters.AddWithValue("@collage", txtcollage.Text);
cmd.Parameters.AddWithValue("@dob", txtdob.Text);
cmd.Parameters.AddWithValue("@address", txtaddress.Text);
cmd.Parameters.AddWithValue("@contact", txtcontact.Text);
cmd.Parameters.AddWithValue("@email", txtemail.Text);
cmd.Parameters.AddWithValue("@technology", txttechnology.Text);
// cmd.Parameters.AddWithValue("@id", txtid.Text);*/
//SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
//cmd.ExecuteNonQuery();
// lblmsg.Text = "Successfully updated";
}
else
{
connection.Close();
}
}
}
按钮:
<asp:Button ID="UPDATE" class="btn btn-info pull-right" runat="server" Text="UPDATE" onClick="UPDATE_Click" ></asp:Button>
据我所见,您没有将在WHERE
部分定义的@id
参数值添加为where id = @id
。看起来您需要取消对该行的注释;
// cmd.Parameters.AddWithValue("@id", txtid.Text);*/
如果不需要,也可以删除命令中的@id
参数。
还有几件事;
- 使用
using
语句自动处理连接和命令,而不是手动调用Close
方法 - 尽量不要使用
AddWithValue
。它有时可能会产生意想不到的结果。使用Add
方法重载来指定参数类型及其大小
using(var connection = new SqlConnection(conStr))
using(var cmd = connection.CreateCommand())
{
// Define your CommandText
// Add your parameter values with Add method
// Open your connection
// Execute your query
}
请注意,您将所有参数添加为文本。根据它的名称,id
列应该是数字类型的,而不是字符类型的。