过去两天我一直遇到以下问题。
我想编辑用户名“Peter Chew”从“Staff”到“Admin”的角色。但它没有从我的数据库填充数据到radiobuttonlist
这是我的数据库表:
这是我的radiobuttonlist:
我的代码:
namespace BookReservation.Staff
{
public partial class EditStaff : System.Web.UI.Page
{
int id = 0;
SqlConnection conn = null;
SqlCommand cmd = null;
string connectionString = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataReader dr = null;
connectionString = ConfigurationManager.ConnectionStrings["LeaveManagementCS"].ConnectionString;
conn = new SqlConnection(connectionString);
string sql = "SELECT * FROM Staff";
try
{
cmd = new SqlCommand(sql, conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
ListItem item = new ListItem(dr["Title"].ToString(), dr["StaffId"].ToString());
ddlTitle.Items.Add(item);
}
dr.Close();
}
catch (Exception ex)
{
lblOutput.Text = "Error Message:" + ex.Message;
}
finally
{
if (conn != null)
conn.Close();
}
if (Request.Params["username"] != null)
{
string username = Request.QueryString["username"];
sql = "SELECT * FROM Staff where Username=@username";
try
{
cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@username", username);
conn.Open();
dr = cmd.ExecuteReader();
dr.Read();
lblIdOut.Text = dr["StaffId"].ToString();
tbUsername.Text = username.ToString();
tbPassword.Text = dr["Password"].ToString();
tbStaffName.Text = dr["StaffName"].ToString();
tbEmail.Text = dr["Email"].ToString();
tbPhone.Text = dr["PhoneNo"].ToString();
dr.Close();
}
catch (Exception ex)
{
lblOutput.Text = "Error Message: " + ex.Message;
}
finally
{
if (conn != null)
conn.Close();
}
}
else
{
lblOutput.Text = "Error. There is no ID to retrieve from the DB.";
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblMessage.Text = "";
lblMessage.Text += "Username " + tbUsername.Text + "<br />";
lblMessage.Text += "Password " + tbPassword.Text + "<br />";
lblMessage.Text += "Staff Name " + tbPassword.Text + "<br />";
lblMessage.Text += "Email " + tbEmail.Text + "<br />";
lblMessage.Text += "Phone No " + tbPhone.Text + "<br />";
lblMessage.Text += "Title " + ddlTitle.SelectedItem.Text + "<br />";
lblMessage.Text += "Role " + rblRole.SelectedItem.Text + "<br />";
connectionString = ConfigurationManager.ConnectionStrings["LeaveManagementCS"].ConnectionString;
conn = new SqlConnection(connectionString);
string sql = "UPDATE Staff SET Username=@user, Password=@Pwd, StaffName=@staff, Email=@email, PhoneNo=@phone, Title=@title, Role=@role ";
sql += " WHERE StaffId=@id";
string ddl = ddlTitle.SelectedItem.Text;
string rbl = rblRole.SelectedItem.Text;
try
{
cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@id", lblIdOut.Text);
cmd.Parameters.AddWithValue("@user", tbUsername.Text);
cmd.Parameters.AddWithValue("@Pwd", tbPassword.Text);
cmd.Parameters.AddWithValue("@staff", tbStaffName.Text);
cmd.Parameters.AddWithValue("@email", tbEmail.Text);
cmd.Parameters.AddWithValue("@phone", tbPhone.Text);
cmd.Parameters.AddWithValue("@title", ddl);
cmd.Parameters.AddWithValue("@role", rbl);
conn.Open();
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
lblOutput.Text = "Record update successfully";
}
}
catch (Exception ex)
{
lblOutput.Text = "Error Message: " + ex.Message;
}
finally
{
if (conn != null)
conn.Close();
}
}
}
}
答案 0 :(得分:0)
要根据当前数据库值设置RadioButtonList的初始值,请在Page.Reoad()中将此行添加到dr.Read()和dr.Close()之间(您要设置其他控件的值):
rblRole.SelectedValue = dr["Role"].ToString();
要根据所选的RadioButtonList项更新数据库值,请从以下位置更改此行:
string rbl = rblRole.SelectedItem.Text;
到此:
string rbl = rblRole.SelectedItem.Value;
SelectedItem.Text属性获取所选项目的可见文本(&#34; Admin&#34;或&#34; Staff&#34;),这是用于向用户显示所选项目的内容。但是,对于数据库条目,您需要所选项目的隐藏值(&#34; A&#34;或&#34; S&#34;),这些值可通过SelectedItem.Value(或SelectedValue)属性访问。 / p>