我正在尝试从单个Web表单保存检索和更新数据表。数据已保存并正确检索但未更新。
这是代码
using System;
using System.Web.UI;
using System.Data;
public partial class Basic_Information : System.Web.UI.Page
{
DataTable user_dt, personal_dt = new DataTable();
string u_id;
Boolean check = false;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
if (Session["user"] != null)
load();
}
protected void btn_save_Click(object sender, EventArgs e)
{
if(check == false)
savedata();
else
editdata();
}
protected void load()
{
user_dt = (DataTable)Session["user"];
u_id = user_dt.Rows[0][0].ToString();
personal_dt = (DataTable)Session["personal"];
if (personal_dt.Rows.Count != 0)
{
retrivedata();
check = true;
}
}
protected void savedata()
{
personal_dt.Columns.Add("User_id");
personal_dt.Columns.Add("Name");
personal_dt.Columns.Add("Father_Name");
personal_dt.Columns.Add("CNIC");
personal_dt.Columns.Add("Date_of_Birth");
personal_dt.Columns.Add("Domicile");
personal_dt.Columns.Add("Gender");
personal_dt.Columns.Add("Religion");
personal_dt.Columns.Add("Nationality");
personal_dt.Columns.Add("Marital");
DataRow per_row = personal_dt.NewRow();
per_row["User_id"] = u_id;
per_row["Name"] = txt_name.Text;
per_row["Father_Name"] = txt_fname.Text;
per_row["CNIC"] = txt_cnic.Text;
per_row["Date_of_Birth"] = txt_dofb.Text;
per_row["Domicile"] = ddl_domicil.SelectedItem.Text;
per_row["Gender"] = ddl_gender.SelectedItem.Text;
per_row["Religion"] = ddl_religion.SelectedItem.Text;
per_row["Nationality"] = txt_nationality.Text;
per_row["Marital"] = ddl_marital.SelectedItem.Text;
personal_dt.Rows.Add(per_row);
string script = "alert('Profile Successfully Saved');";
ClientScript.RegisterStartupScript(this.GetType(), "saved", script, true);
}
}
protected void retrivedata()
{
txt_name.Text = personal_dt.Rows[0]["Name"].ToString();
txt_fname.Text = personal_dt.Rows[0]["Father_Name"].ToString();
txt_cnic.Text = personal_dt.Rows[0]["CNIC"].ToString();
txt_dofb.Text = personal_dt.Rows[0]["Date_of_Birth"].ToString();
ddl_domicil.Items.FindByText(personal_dt.Rows[0] ["Domicile"].ToString()).Selected = true;
ddl_gender.Items.FindByText(personal_dt.Rows[0]["Gender"].ToString()).Selected = true;
ddl_religion.Items.FindByText(personal_dt.Rows[0]["Religion"].ToString()).Selected = true;
txt_nationality.Text = personal_dt.Rows[0]["Nationality"].ToString();
ddl_marital.Items.FindByText(personal_dt.Rows[0]["Marital"].ToString()).Selected = true;
}
protected void editdata()
{
personal_dt.Rows[0]["Name"] = txt_name.Text;
personal_dt.Rows[0]["Father_Name"] = txt_fname.Text;
personal_dt.Rows[0]["CNIC"] = txt_cnic.Text;
personal_dt.Rows[0]["Date_of_Birth"] = txt_dofb.Text;
personal_dt.Rows[0]["Domicile"] = ddl_domicil.SelectedItem.Text;
personal_dt.Rows[0]["Gender"] = ddl_gender.SelectedItem.Text;
personal_dt.Rows[0]["Religion"] = ddl_religion.SelectedItem.Text;
personal_dt.Rows[0]["Nationality"] = txt_nationality.Text;
personal_dt.Rows[0]["Marital"] = ddl_marital.SelectedItem.Text;
}
}
正确保存数据,正确检索数据。
但编辑数据中的错误会返回检索到的第一个值。
也是Dropdown无法选择多个项目的错误
答案 0 :(得分:0)
关于下拉列表绑定问题,之前
Dropdown.DataSource = YourDataSource
加
Dropdown.ClearSelection();
答案 1 :(得分:0)
试试这个。在将数据填充到数据表后,您需要传递该数据表,并更新记录。
protected void editdata()
{
DataRow dr = personal_dt.NewRow();
personal_dt.Rows.Add(dr);
dr["Name"] = txt_name.Text;
dr["Father_Name"] = txt_fname.Text;
dr["CNIC"] = txt_cnic.Text;
dr["Date_of_Birth"] = txt_dofb.Text;
dr["Domicile"] = ddl_domicil.SelectedItem.Text;
dr["Gender"] = ddl_gender.SelectedItem.Text;
dr["Religion"] = ddl_religion.SelectedItem.Text;
dr["Nationality"] = txt_nationality.Text;
dr["Marital"] = ddl_marital.SelectedItem.Text;
}