Click here to Skip to main content
14,974,887 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?
vikasvanvi 7-Dec-13 11:36am
   
same as when i put gridview before delete method ,nothing changes
JoCodes 8-Dec-13 2:57am
   
You also dont have a CommandArgument set inside gridview column?
vikasvanvi 8-Dec-13 3:41am
   
no,what to write in CommandArgument?
JoCodes 8-Dec-13 7:17am
   
"<%# Eval("fieldvalue" )%>
JoCodes 8-Dec-13 7:19am
   
Then only the e.CommandArgument can be retrieved in your rowcammand event
vikasvanvi 8-Dec-13 9:30am
   
thnks,will try
footballpardeep 7-Dec-13 14:40pm
   
Can you show your aspx code.
vikasvanvi 8-Dec-13 2:28am
   
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="Gridview1_RowCommand" >
<alternatingrowstyle backcolor="White" forecolor="#284775">
<columns>
<asp:BoundField DataField="picID" HeaderText="pic id" />
<asp:BoundField DataField="title" HeaderText="title" />
<asp:BoundField DataField="price" HeaderText="price" />
<asp:BoundField DataField="dateAdded" HeaderText="date" />
<asp:ImageField DataImageUrlField="picURL" HeaderText="image">

<asp:ButtonField ButtonType="Button" Text="remove" CommandName="removeRow" />

<editrowstyle backcolor="#999999">
<footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White">
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<rowstyle backcolor="#F7F6F3" forecolor="#333333">
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<sortedascendingcellstyle backcolor="#E9E7E2">
<sortedascendingheaderstyle backcolor="#506C8C">
<sorteddescendingcellstyle backcolor="#FFFDF8">
<sorteddescendingheaderstyle backcolor="#6F8DAE">


 </div>
<asp:Label ID="Label1" runat="server">
</form>
</body>

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..
   
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();


}

}
   
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