i have one datalist having shopping cart table which retrieves values as per customer_id

aspx code.

            <asp:DataList ID="DataList1" runat="server" DataKeyField="Customer_Product" 
        DataSourceID="SqlDataSource2" onupdatecommand="DataList1_UpdateCommand" 
        oneditcommand="DataList1_EditCommand1" 
        ondeletecommand="DataList1_DeleteCommand" 
                oncancelcommand="DataList1_CancelCommand1">
        <HeaderTemplate>  
    <table border="solid 1px black">  
        <tr>  
            <th style="width: 250px">  

            </th>  
            <th style="width: 400px">  
                Item Description  
            </th> 
            <th style="width: 100px">  
                Price
            </th> 
            <th style="width: 100px">  
                Qty
            </th>  
            <th style="width: 100px">  
                Remove
            </th>  
                <th style="width: 100px">  
                Total
            </th>  
        </tr>  
</HeaderTemplate> 
        <ItemTemplate>
         <tr>  
        <td> 
            <asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Product_Image") %>' Width="100px" Height="150px" />  
        </td>  
        <td>  
            <%# DataBinder.Eval(Container.DataItem, "Item_Description") %>  
        </td>  
        <td>  
            <%# DataBinder.Eval(Container.DataItem, "Price") %>  
        </td>  
        <td>  
            <%# DataBinder.Eval(Container.DataItem, "Qty") %>  
            <br />
                                            <asp:LinkButton ID="lnkEdit"   
                runat="server"   
                CommandName="edit">  
                Change  
            </asp:LinkButton>  

        </td>  
        <td>  
            <asp:LinkButton ID="LinkButton1"   
                runat="server"   
                CommandName="delete">  
                Delete
            </asp:LinkButton>  
        </td>  
       <td>
           <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Total") %>  '></asp:Label></td>
    </tr>  
        </ItemTemplate>
        <EditItemTemplate>  
    <tr>  
        <td>  
            <asp:Image ID="Image2" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Product_Image") %>' Width="100px" Height="150px" />  
        </td>
        <td>  
             <%# DataBinder.Eval(Container.DataItem, "Item_Description") %>  
        </td>
        <td>
        <asp:Label ID="price" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Price") %> '></asp:Label> 
        </td>  
        <td>  
           <asp:TextBox ID="TextBox1"   
                runat="server"   
                Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>'>
            </asp:TextBox>  

            <asp:LinkButton ID="lnkUpdate"   
                runat="server"   
                CommandName="Update">  
                Update  
            </asp:LinkButton>  
            <asp:LinkButton ID="lnkCancel"   
                runat="server"   
                CommandName="cancel">  
                Cancel  
            </asp:LinkButton>  
        </td>  
            <td></td>
    </tr>  
</EditItemTemplate>  
<FooterTemplate>  
    </table>  
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</FooterTemplate> 
    </asp:DataList>
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
         ConnectionString="<%$ ConnectionStrings:OnlineShopConnectionString2 %>" 

            SelectCommand="SELECT * FROM [Cart1] WHERE ([Customer_ID] = @Customer_ID)" 
         UpdateCommand="UPDATE [Cart1] SET [Qty] = @Qty,[Total]=@Total WHERE [Customer_Product] = @original_C_P"

         DeleteCommand="delete from [Cart1] WHERE [Customer_Product] = @original_C_P" >
         <SelectParameters>
             <asp:QueryStringParameter Name="Customer_ID" QueryStringField="name" 
                 Type="Int32" />
         </SelectParameters>
         <DeleteParameters>
          <asp:Parameter Name="original_C_P" Type="String" />
         </DeleteParameters>
        <UpdateParameters>
           <asp:Parameter Name="Qty" Type="String" />
           <asp:Parameter Name="Total" Type="String" />


           <asp:Parameter Name="original_C_P" Type="String" />
        </UpdateParameters></asp:SqlDataSource>

and using edit delete update command customer can make changes to product

my cs code....

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Customer_ID"] == "")
        {
            Response.Redirect("LogIn.aspx");
        }
        else
        {
            string name=Request.QueryString["name"];
            string id = Session["Customer_ID"].ToString();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=HP-HP\\SQLEXPRESS;Initial Catalog=OnlineShop;Persist Security Info=True;User ID=sa;Password=rane@1234";
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Cart1 where Customer_ID='"+name+"'", con);



            SqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                DataList1.DataSourceID = null;
                DataList1.DataSource = dt;
                DataList1.DataBind();
            con.Close();
        }


    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)

        {
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string id = Session["Customer_ID"].ToString();
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=HP-HP\\SQLEXPRESS;Initial Catalog=OnlineShop;Persist Security Info=True;User ID=sa;Password=rane@1234";
        con.Open();
        SqlCommand cmd = new SqlCommand("Delete from Cart1 where Customer_ID='" + id + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        DataList1.DataSourceID = null;
        DataList1.DataSource = dt;
        DataList1.DataBind();
        con.Close();

    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        String Qty = ((TextBox)e.Item.FindControl("TextBox1")).Text;
        String Price = ((Label)e.Item.FindControl("price")).Text;
        int total = (Convert.ToInt32(Qty) * Convert.ToInt32(Price));
        String Total1 = total.ToString();
        String Customer_Product = DataList1.DataKeys[e.Item.ItemIndex].ToString();        


        SqlDataSource2.UpdateParameters["Qty"].DefaultValue = Qty;
        SqlDataSource2.UpdateParameters["Total"].DefaultValue = Total1;
        SqlDataSource2.UpdateParameters["original_C_P"].DefaultValue = Customer_Product;



        DataList1.EditItemIndex = -1;
        DataList1.DataBind();

    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        String Customer_ID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
        String Product_ID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
        String Product_Image = ((Image)e.Item.FindControl("Image1")).ImageUrl;


        SqlDataSource2.DeleteParameters["original_CustomerID"].DefaultValue = Customer_ID;
        SqlDataSource2.DeleteParameters["Product_Image"].DefaultValue = Product_Image;
        SqlDataSource2.Delete();

        DataList1.EditItemIndex = -1;
        DataList1.DataBind();

    }
    protected void DataList1_EditCommand1(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataBind();

    }
    protected void DataList1_CancelCommand1(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        DataList1.DataBind();

    }

edit cancel delete works fine but when i try to update the product it not making any change to the database.
if a customer have buy one shirts if he want to make it 5 the database quantity and total should get updates.

code for update command

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
    String Qty = ((TextBox)e.Item.FindControl("TextBox1")).Text;
    String Price = ((Label)e.Item.FindControl("price")).Text;
    int total = (Convert.ToInt32(Qty) * Convert.ToInt32(Price));
    String Total1 = total.ToString();
    String Customer_Product = DataList1.DataKeys[e.Item.ItemIndex].ToString();        

    SqlDataSource2.UpdateParameters["Qty"].DefaultValue = Qty;
    SqlDataSource2.UpdateParameters["Total"].DefaultValue = Total1;
    SqlDataSource2.UpdateParameters["original_C_P"].DefaultValue = Customer_Product;

    DataList1.EditItemIndex = -1;
    DataList1.DataBind();

}


i know its bit comple but help me out.

You can't use both - DataSource control (codeless approach) and Code methods to handle events of DataList control. Choose either one of them.

If you've plan to use code then use IsPostBack property in page_load to populate the DataList control.

Something like this:

if(!IsPostBack)
{
  string name=Request.QueryString["name"];
  string id = Session["Customer_ID"].ToString();
  SqlConnection con = new SqlConnection();
  con.ConnectionString = "Data Source=HP-HP\\SQLEXPRESS;Initial Catalog=OnlineShop;Persist Security Info=True;User ID=sa;Password=rane@1234";
  con.Open();
  SqlCommand cmd = new SqlCommand("select * from Cart1 where Customer_ID='"+name+"'", con);
  SqlDataReader dr = cmd.ExecuteReader();
  DataTable dt = new DataTable();
  dt.Load(dr);
  DataList1.DataSourceID = null;
  DataList1.DataSource = dt;
  DataList1.DataBind();
  con.Close();
}

I'd like to suggest you that you should dispose the ADO.NET objects properly by using using block.

using(SqlConnection con = new SqlConnection())
 {
   con.ConnectionString = "Data Source=HP-HP\\SQLEXPRESS;Initial Catalog=OnlineShop;Persist Security Info=True;User ID=sa;Password=rane@1234";
  using(SqlCommand cmd = new SqlCommand("select * from Cart1 where Customer_ID=@name", con))
    {
    cmd.Parameters.AddWithValue("@name",name); 
    SqlDataReader dr = cmd.ExecuteReader();
    ...
    }
  }
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.