У меня были проблемы со следующей проблемой за последние 2 дня.Как заполнить RadioButtonList из моей базы данных
Я хочу изменить имя пользователя «Peter Chew» из «Персонала» в «Администратор». Но это не заселить данные в 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 [email protected]";
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 [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] ";
sql += " WHERE [email protected]";
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();
}
}
}
}
Каков идентификатор вашего RadioButtonList? – Kami