在此我试图用输入更新用户表单。但提交表格后没有得到更新&显示其中的旧数据。
vb代码
Private Sub updateInfo_Click(sender As Object, e As EventArgs) Handles updateInfo.Click
Try
Dim con As New MySqlConnection
Dim query As New MySqlCommand
con.ConnectionString = ConfigurationManager _
.ConnectionStrings("conio").ConnectionString()
query.Connection = con
con.Open()
query.CommandText = "UPDATE hospitals SET name = '" & businessName.Text & "', contactPerson = '" + contactPerson.Text + "', websiteName = '" + websiteName.Text + "', email = '" + emailName.Text + "', phone1 = '" + phone1.Text + "', phone2 = '" + phone2.Text + "', mobileNumber = '" + mobile.Text + "', buildingName = '" + buildingName.Text + "', streetName = '" + address.Text + "', landmark = '" + landmark.Text + "', areaName = '" + areaName.Text + "', city = '" + suburb.Text + "', state = '" + state.Text + "', zipCode = '" + zip.Text + "', overview = '" + overview.Text + "', registration = '" + regNo.Text + "', establishment = '" + foundation.Text + "', founder = '" + founderName.Text + "', generalBed = '" + GeneralBeds.Text + "', icuBed = '" + ICU.Text + "', consultancyFees = '" + consultinfees.Text + "', mondayFrom = '" + mondayFrom.Text + "', mondayTo = '" + mondayTo.Text + "', tuesdayFrom = '" + tuesdayFrom.Text + "', tuesdayTo = '" + tuesdayTo.Text + "', wednesdayFrom = '" + wedFrom.Text + "', wednesdayTo = '" + wedTo.Text + "', thursdayFrom = '" + thursdayFrom.Text + "', thursdayTo = '" + thursdayTo.Text + "', fridayFrom = '" + fridayFrom.Text + "', fridayTo = '" + fridayTo.Text + "', saturdayFrom = '" + saturdayFrom.Text + "', saturdayTo = '" + saturdayTo.Text + "', sundayFrom = '" + sundayFrom.Text + "', sundayTo = '" + sundayTo.Text + "', visitFrom = '" + visitFrom.Text + "', visitTo = '" + visitTo.Text + "', bestKnownFor = '" + bestknowFor.Text + "' WHERE hospitalID = '" & TextId.Text + "'"
query.ExecuteNonQuery()
con.Close()
Response.Write("<script language='javascript'>alert('Information updated successfully.');</script>")
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
因此它也没有显示任何错误。请帮助确定问题。
更新
If IsPostBack Then
Try
Dim str As String = "SELECT * FROM hospitals WHERE username='" + Server.HtmlEncode(Request.Cookies("chkusername").Value) + "';"
con.Open()
Dim cmd As New MySqlCommand(str, con)
Dim da As New MySqlDataAdapter(cmd)
Dim dt As New DataTable
Dim lblservice As New Label
For Each chk As ListItem In servicesList.Items
If chk.Selected = True Then
lblservice.Text = String.Concat(lblservice.Text + ",", chk.Value)
End If
Next
da.Fill(dt)
con.Close()
TextId.Text = dt.Rows(0)("hospitalID").ToString
businessName.Text = dt.Rows(0)("name").ToString
welcome.Text = dt.Rows(0)("name").ToString
subCat.Text = dt.Rows(0)("subcategory").ToString
contactPerson.Text = dt.Rows(0)("contactPerson").ToString
websiteName.Text = dt.Rows(0)("websiteName").ToString
emailName.Text = dt.Rows(0)("email").ToString
phone1.Text = dt.Rows(0)("phone1").ToString
phone2.Text = dt.Rows(0)("phone2").ToString
mobile.Text = dt.Rows(0)("mobileNumber").ToString
buildingName.Text = dt.Rows(0)("buildingName").ToString
address.Text = dt.Rows(0)("streetName").ToString
landmark.Text = dt.Rows(0)("Landmark").ToString
areaName.Text = dt.Rows(0)("areaName").ToString
suburb.Text = dt.Rows(0)("city").ToString
state.Text = dt.Rows(0)("state").ToString
zip.Text = dt.Rows(0)("zipCode").ToString
overview.Text = dt.Rows(0)("overview").ToString
regNo.Text = dt.Rows(0)("registration").ToString
foundation.Text = dt.Rows(0)("establishment").ToString
founderName.Text = dt.Rows(0)("Founder").ToString
GeneralBeds.Text = dt.Rows(0)("generalBed").ToString
ICU.Text = dt.Rows(0)("icuBed").ToString
consultinfees.Text = dt.Rows(0)("consultancyFees").ToString
mondayFrom.Text = dt.Rows(0)("mondayFrom").ToString
mondayTo.Text = dt.Rows(0)("mondayTo").ToString
tuesdayFrom.Text = dt.Rows(0)("tuesdayFrom").ToString
tuesdayTo.Text = dt.Rows(0)("tuesdayTo").ToString
wedFrom.Text = dt.Rows(0)("wednesdayFrom").ToString
wedTo.Text = dt.Rows(0)("wednesdayTo").ToString
thursdayFrom.Text = dt.Rows(0)("thursdayFrom").ToString
thursdayFrom.Text = dt.Rows(0)("thursdayFrom").ToString
fridayFrom.Text = dt.Rows(0)("fridayFrom").ToString
fridayTo.Text = dt.Rows(0)("fridayTo").ToString
saturdayFrom.Text = dt.Rows(0)("saturdayFrom").ToString
saturdayTo.Text = dt.Rows(0)("saturdayTo").ToString
sundayFrom.Text = dt.Rows(0)("sundayFrom").ToString
sundayTo.Text = dt.Rows(0)("sundayTo").ToString
visitFrom.Text = dt.Rows(0)("visitFrom").ToString
visitTo.Text = dt.Rows(0)("visitTo").ToString
bestknowFor.Text = dt.Rows(0)("bestKnownFor").ToString
Catch ex As Exception
Response.Write(ex)
End Try
End If
答案 0 :(得分:0)
private static string item_id = "-1";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
item_id = Request.QueryString["id"].ToString();
RestoreItemData(item_id);
}
}
protected void RestoreItemData(string id)
{
SqlConnection con = new SqlConnection(Utils.Connection);
SqlCommand cmd = new SqlCommand("items_sel", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
txtName.Text = sdr["name"].ToString();
}
catch (Exception ex)
{
//...
}
finally
{
con.Close();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Utils.Connection);
SqlCommand cmd = new SqlCommand("items_iu", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = item_id;
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = txtName.Text.ToString();
cmd.Parameters.Add("@supitem_id", SqlDbType.Int).Value = Session["supitem"].ToString();
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
//...
}
finally
{
con.Close();
Response.Redirect("Items_list.aspx");
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Utils.Connection);
SqlCommand cmd = new SqlCommand("items_d", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = item_id;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
//...
}
finally
{
con.Close();
Response.Redirect("Items_list.aspx");
}
}
这只是一个例子,正如您所要求的那样。 让我解释。当它是初始页面加载时,阻止 if if(!IsPostBack)将首先执行。
让我举个例子。你有一份医院名单。当您点击任何医院时,应显示有关医院的详细信息,您必须能够按要求修改数据。
那么 if(!IsPostBack)会怎么做?
让我们假设您的医院只有一列: name ,您有一些名为 hospital_name 的字符串变量。让我告诉你解决方案中的所有步骤......你有一家名为 Virginia 的医院。您的解决方案:
1) hospital_name =“弗吉尼亚”;
2)您正在更新名称为“Virginia1”。现在 hospital_name =“Virginia1”。然后点击按钮
3)你的page_load首先出现,现在再次出现 hospital_name =“Virginia”,caz'它来自数据库
4)你的button_click方法会触发,但会将值更新为旧的。
因此,如果您要从PostBack内的数据库中移动检索,则流程如下:
1) hospital_name =“弗吉尼亚”
2)你改变了它的价值: hospital_name =“Virginia1”
3)你的page_load触发,但是从数据库中检索是在 if(!IsPostBack)中,但是按钮的点击将触发回发,因此从数据库中检索将不起作用。所以你的 hospital_name 仍然等于“Virginia1”
4)按钮单击方法触发并将数据库的值更改为“Virginia1”
希望你明白这一点。