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

Recommended Answers

All 3 Replies

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";
           }
        }
    } 	
  .....

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

SelectedValue property to select an item from RadioButtonList

stauts.SelectedValue=dr["status"].ToString();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.