3
Reply

my EditStaff problem

Ask a question
Cj Mah

Cj Mah

8y
292
1
the problem is that i put the query string as StaffId not Id. it give me this error, Invalid attempt to read when no data is present. So help me fixed the problem.
 
 
string staffId = null;
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["FacilityCS"].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["StaffId"] != null)
{
staffId = Convert.ToString(Request.Params["StaffId"]);
sql = "SELECT * from Staff";
try
{
cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@StaffId", staffId);
conn.Open();
dr = cmd.ExecuteReader();
tbStaffId.Text = dr["StaffId"].ToString();
tbPassword.Text = dr["Password"].ToString();
tbStaffName.Text = dr["StaffName"].ToString();
tbEmail.Text = dr["Email"].ToString();
tbPhoneNo.Text = dr["PhoneNo"].ToString();
ddlTitle.SelectedValue = dr["Title"].ToString();
rbRole.SelectedValue = dr["Role"].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.";
}
}

Answers (3)