Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
3.78/5 (3 votes)
i wants to delete gridview row using command name so
when i debug , from gridviewbind();
deleteCart(pic);

it stops,and it is not entering into public void deleteCart(string orderID), what could be a problem?



protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gridviewbind();
        }
    }

public void gridviewbind()
    {
        SqlConnection con = new SqlConnection("constring1");
        string str = "select * from Products INNER JOIN orders on Products.picID=orders.productID";
        SqlCommand cmd = new SqlCommand(str, con);
        con.Open();
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("picID", typeof(int)));
        dt.Columns.Add(new DataColumn("title", typeof(string)));
        dt.Columns.Add(new DataColumn("price", typeof(int)));
        dt.Columns.Add(new DataColumn("dateAdded", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("picURL", typeof(string)));
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            DataRow dr = dt.NewRow();
            dr["picID"] = Convert.ToInt32(reader["picID"]);
            dr["title"] = reader["title"];
            dr["price"] = Convert.ToInt32(reader["price"]);
            dr["dateAdded"] = reader["dateAdded"];
            dr["picURL"] = ResolveUrl("~/images/" + reader["picURL"]);
            dt.Rows.Add(dr);
        }
        reader.Close();
        con.Close();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
   {

       if (e.CommandName == "removeRow")
       {
           string pic = Convert.ToString(e.CommandArgument);
           //  GridViewRow row = GridView1.Rows[pic];

           //     deleteCart(row.Cells[1].Text.ToString());
           gridviewbind();
           deleteCart(pic);

       }

   }


public void deleteCart(string orderID)
    {
        using (SqlConnection con = new SqlConnection("constring1"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "delete from orders where orderID=@orderID";
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@orderID", orderID);
                con.Open();
                var temp = cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
Posted
Updated 7-Dec-13 5:13am
v2
Comments
JoCodes 7-Dec-13 10:50am    
You need to call the delete method before the gridview bind method
vikasvanvi 7-Dec-13 10:56am    
already tried,not working
JoCodes 7-Dec-13 10:59am    
in that case its not even entering into the method?
vikasvanvi 7-Dec-13 11:00am    
no,it stops at grdviewbind()
JoCodes 7-Dec-13 11:06am    
not that. what happens if placing delete method before gridview bind method?

XML
Hi Friend....

Your seem good.... I would like to suggest you that please check whether following lines of code passing EXACT values as you want or NOT....

Use Degubber to do that....

<pre lang="c#">if (e.CommandName == "removeRow")
{
string pic = Convert.ToString(e.CommandArgument);

gridviewbind();
deleteCart(pic);

}

......
And also...put the function deleteCart(pic)above the function gridviewbind()


Then check the result..
 
Share this answer
 
v2
Comments
vikasvanvi 7-Dec-13 11:35am    
yes it is passing same value that i wants,and i have placed deleteCart(pic) above gridviewbind() but debuging stops at gridviewbind()and shows webpage on browser without doing anything
MayurDighe 7-Dec-13 11:42am    
You...it Debugging will STOP ....if you don't go further inside the function....

when you come up to the line "deleteCart(pic);" ....just go inside the function by pressing Keystroke....generally you'll find it at DEBUG menu...

just check...it..and TRY....

Happy Programming.....:-)
In your code add commandArgument:

<asp:ButtonField ButtonType="Button" Text="remove" CommandName="removeRow" CommandArgument='<%# Eval("picID") %>' />


and


protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{

if (e.CommandName == "removeRow")
{
string picID = Convert.ToString(e.CommandArgument);

// derive orderId from picID, as from code, as there is no orderId in Cell[1] of row, in cell[1] you have title of product

deleteCart(orderID);

gridviewbind();


}

}
 
Share this answer
 
Comments
vikasvanvi 8-Dec-13 9:45am    
thank you very much,worked :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900