0

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...

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by __avd
0

Advice: Use parameterized queries.

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin"] == null)
        {
            Response.Redirect("Default.aspx?msg=1");
        }
        if (string.IsNullOrEmpty(Request.QueryString["id"]) && btn_addcategory.Text!="update")
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = helper.ConnectionString;
            string edit = Request.QueryString["id"];
            SqlCommand cmd = new SqlCommand("select * from category where id=@id", conn);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@id",edit); 
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
   
            if(dr.Read()) {
             cat_name.Text = Convert.ToString(dr["name"]);
             //status.SelectedItem.Value = Convert.ToString(dr["status"]);
             btn_addcategory.Text = "update";
           }
        }
    } 	
  .....
0

thanks for the reply...but this one for the radiobuttons value seems not working.. status.SelectedItem.Value = Convert.ToString(dr["status"]);

0

SelectedValue property to select an item from RadioButtonList

stauts.SelectedValue=dr["status"].ToString();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.