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
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)
{
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;
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
<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>