Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i'm a beginner here
i am updating my product name and price
but if i only update my price, my product name and images went missing
only the field that i update will appear.

could anyone help mi out? ><<br mode="hold" />
this is my current code

in AdminProduct.aspx.cs

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            listbind();
        }
    }

    private void listbind()
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);
        SqlCommand cmd = new SqlCommand("SELECT ProductID, ProductName, Price, Description, Picture FROM Product", myConnect);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Product");
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }





    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        //call edit item template for selected index
        DataList1.EditItemIndex = e.Item.ItemIndex;
        listbind();
    }

    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);
        int ProductID = (int)DataList1.DataKeys[(int)e.Item.ItemIndex];

        string strCommandText = "DELETE from Product WHERE ProductID=" + ProductID;
        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

        myConnect.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        myConnect.Close();
        DataList1.EditItemIndex = -1;
        listbind();

        
        
    }

    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;//Call Item Template
        listbind();
    }

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {

        int ProductID = Convert.ToInt32(Request.QueryString["ProductID"]);
        string Picture = "~/Image/" + Request.QueryString["Picture"];
        string ProductName;
        decimal Price;

        ProductID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
        ProductName = ((TextBox)(e.Item.FindControl("txtProductName"))).Text;
        Price = Convert.ToDecimal(((TextBox)(e.Item.FindControl("txtPrice"))).Text);
        
        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "UPDATE Product " + " SET ProductName=@aProductName, " + " Price=@aPrice, " + " Picture=@aPicture" + " WHERE ProductID=@aProductID ";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

        cmd.Parameters.Add("@aProductID", SqlDbType.Int).Value = ProductID;
        cmd.Parameters.Add("@aProductName", SqlDbType.NVarChar, 50).Value = ProductName;
        cmd.Parameters.Add("@aPrice", SqlDbType.Decimal).Value = Price;
        cmd.Parameters.Add("@aPicture", SqlDbType.NVarChar, 100).Value = Picture;

        myConnect.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        myConnect.Close();
        DataList1.EditItemIndex = -1;
        listbind();
    }


in AdminProduct.aspx

C#
<asp:DataList ID="DataList1" runat="server" CellPadding="4" 
        RepeatColumns="4" Width="1042px" 
        BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" 
        GridLines="Both" Height="369px" 
        OnCancelCommand="DataList1_CancelCommand" 
        OnEditCommand="DataList1_EditCommand"
        OnUpdateCommand="DataList1_UpdateCommand" 
        OnDeleteCommand="DataList1_DeleteCommand"
        DataKeyField="ProductID" RepeatDirection="Horizontal"
        >

        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <ItemStyle BackColor="White" ForeColor="#330099" HorizontalAlign="Center"/>
        
        
        <ItemTemplate>


            <asp:HyperLink ID="hlnk" NavigateUrl='<%# string.Format("ViewProductDetail.aspx?ProductID={0}&Picture={1}", Eval("ProductID"), Eval("Picture")) %>' runat="server">
                    
             <asp:Image ID="Image1" runat="server" Height="127px" 
                    ImageUrl='<%# Bind("Picture", "~/Image/{0}") %>' Width="129px" />
                    
                    </asp:HyperLink>
            <br />
            <asp:Label ID="ProductNameLabel" runat="server" 
                Text='<%# Eval("ProductName") %>' />
            <br />
            <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price","{0:C}") %>' />
            <br />

            <asp:LinkButton ID="b1" Text="Edit" CommandName="edit" runat="server" />
            <asp:LinkButton ID="b2" Text="Delete" CommandName="delete" runat="server" />

            <br />
            <br />
            <br />

        </ItemTemplate>


        <EditItemTemplate>
        <asp:HyperLink ID="hlnk" NavigateUrl='<%# string.Format("ViewProductDetail.aspx?ProductID={0}&Picture={1}", Eval("ProductID"), Eval("Picture")) %>' runat="server">
             <asp:Image ID="Image1" runat="server" Height="127px" 
                    ImageUrl='<%# Bind("Picture", "~/Image/{0}") %>' Width="129px" />
                    
                    </asp:HyperLink>
            <br />
            <asp:Label ID="ProductNameLabel" runat="server" 
                Text='<%# Eval("ProductName") %>' />
             
            <asp:TextBox ID="txtProductName" runat="server"></asp:TextBox>
            <br />
            <asp:Label ID="PriceLabel" runat="server" 
                Text='<%# Eval("Price","{0:C}") %>' />
            
            <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
            <br />
            <asp:LinkButton ID="b4" Text="Update" CommandName="update" runat="server" />
            <asp:LinkButton ID="b3" Text="Cancel" CommandName="cancel" runat="server" />
            <br />
            </EditItemTemplate>


        <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
    </asp:DataList>



    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=x;User ID=x;Password=x" 
        ProviderName="System.Data.SqlClient" 
        
        SelectCommand="SELECT [ProductName], [Price], [Picture], [ProductID] FROM [Product]">
    </asp:SqlDataSource>
Posted

1 solution

In code, you are updating Picture Like this...
C#
string Picture = "~/Image/" + Request.QueryString["Picture"];

So, for instance, the Picture will become ~/Image/somImage.jpg.

But you are binding it like...
XML
<asp:Image ID="Image1" runat="server" Height="127px" ImageUrl='<%# Bind("Picture", "~/Image/{0}") %>' Width="129px" />

So, ImageUrl becomes something like ~/Image/~/Image/somImage.jpg.

That's why it is not loading any image.
You will get clarified of the URL if you look at the rendered HTML in Browser and locate the image.
 
Share this answer
 
Comments
shiiny 19-Dec-13 2:05am    
but my ProductName Text also disappear if i never update it
You have used Eval for Product Name field in ItemTemplate and EditItemTemplate.

<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' />

But it is always suggested to use Bind method inside EditItemTemplate. So, do like below inside EditItemTemplate...

<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Bind("ProductName") %>' />
shiiny 19-Dec-13 2:38am    
change Eval to Bind but it still the same
What exactly is the problem? I am not getting you. Please clarify.
shiiny 19-Dec-13 2:55am    
[x] <- picture
namelabel
pricelabel

after click edit

[x] <- picture
namelabel [textbox]
pricelabel [textbox]

i only edit pricelabel textbox and leave namelabel empty
after edit finish

[x] <- disappear
namelabel <- disappear
pricelabel <- successfully updated

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