hi all,
i am new to asp and i have a problem in editing the data...here is the code can someone please help...
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] == null)
{
Response.Redirect("Default.aspx?msg=1");
}
if (Request.QueryString["id"] != null)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = helper.ConnectionString;
string edit = Request.QueryString["id"];
SqlCommand cmd = new SqlCommand("select * from category where id='" + edit + "'", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
cat_name.Text = Convert.ToString(dr["name"]);
//status.SelectedItem.Value = Convert.ToString(dr["status"]);
btn_addcategory.Text = "update";
}
}
protected void btn_addcategory_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = helper.ConnectionString;
if (Request.QueryString["id"] ==null)
{
SqlCommand cmd = new SqlCommand("insert into category (name,status) values('" + cat_name.Text + "','" + status.Text + "')", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() != null)
{
Response.Redirect("category.aspx");
}
}
else {
string edit = Request.QueryString["id"];
SqlCommand cmd = new SqlCommand("update category set name='" + cat_name.Text + "',status='" + status.Text + "' where id='"+edit+"'", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
cat_name.Text = Convert.ToString(dr["name"]);
status.SelectedItem.Value = Convert.ToString(dr["status"]);
btn_addcategory.Text = "update";
}
}
it throws a System.InvalidOperationException: Invalid attempt to read when no data is present.
thanks...