Orion Orion - 3 months ago 12
ASP.NET (C#) Question

Gridview value passing to next page and perform update stuff

I am using 3 tier architecture in my website. Instead of updating in gridview, I would like to send gridview row values to other page and perform update stuff there. Here is the link code in view page.

Update Product Page Load Code:

protected void Page_Load(object sender, EventArgs e)
{
int productId = 0;
productId = Convert.ToInt32(Request.QueryString["ProductID"]);
ProductBL.GetProductById(productId);
}


Get Product By ID function code:

public static Product GetProductById(int productID)
{
Product product;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select ProductName, Description, ItemsInSet, UnitPriceOwner, UnitPriceReseller, ShippingCost from Products where ProductID = @ProductID";

cmd.Parameters.AddWithValue("@ProductID", SqlDbType.Int).Value = productID;

SqlDataReader reader = DbUtility.GetDataReader(cmd);

if (reader.HasRows)
{
product = new Product();
reader.Read();
product.ProductName = reader.GetString(0);
product.Description = reader.GetString(1);
product.ItemsInSet = reader.GetInt32(2);
product.UnitPriceOwner = reader.GetInt32(3);
product.UnitPriceReseller = reader.GetInt32(4);
product.ShippingCost = reader.GetInt32(5);
return product;
}
else
{
return null;
}
}


UpdateProduct Page:

<label><b>Title</b></label>
<asp:TextBox ID="TitleTextBox" runat="server" placeholder="Enter Product Title"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ControlToValidate="TitleTextBox"
ErrorMessage="Product Name is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>


<label><b>Description</b></label>
<asp:TextBox ID="DescriptionTextBox" runat="server" placeholder="Description"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server"
ControlToValidate="TitleTextBox"
ErrorMessage="Product Name is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>

<label><b>Set of</b></label>
<asp:TextBox ID="SetTextBox" runat="server" placeholder="Enter quantity"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server"
ControlToValidate="SetTextBox"
ErrorMessage="Quantity is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>

<label><b>Unit Price (Owner)</b></label>
<asp:TextBox ID="UnitOwnerTextBox" runat="server" placeholder="Unite Price Owner"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server"
ControlToValidate="UnitOwnerTextBox"
ErrorMessage="Unit Price Owner is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>

<label><b>Unit Price (Reseller)</b></label>
<asp:TextBox ID="UnitResellerTextBox" runat="server" placeholder="Unite Price Reseller"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator5" runat="server"
ControlToValidate="UnitResellerTextBox"
ErrorMessage="Unit Price Reseller is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>

<label><b>Choose Category</b></label><br /><br />
<asp:DropDownList ID="CategoryDropDownList" runat="server" placeholder="Select Category" DataSourceID="SqlDataSource1"
DataTextField="CategoryName" DataValueField="CategoryID" ></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:UmmatiConStr %>" SelectCommand="SELECT [CategoryName], [CategoryID] FROM [Categories]"></asp:SqlDataSource>
<br /><br /><hr />

<label><b>Shipment Cost</b></label>
<asp:TextBox ID="ShipmentTextBox" runat="server" placeholder="Enter Shipment Charge"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator6" runat="server"
ControlToValidate="ShipmentTextBox"
ErrorMessage="Shipment Cost is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>


Can Anyone help how to bind data in textboxes?

public partial class UpdateProduct : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int productId = 0;
productId = Convert.ToInt32(Request.QueryString["ProductID"]);

LoadProductDetails(productId);
}

protected void LoadProductDetails(int productId)
{
ProductBL.GetProductById(productId);

Product product = new Product();
TitleTextBox.Text = product.ProductName;
}


Here, Product name is null while debugging the code.

Answer

You need to set the TextBox like below

TitleTextBox.Text = product.ProductName

Assigning can be done in the PageLoad

Based on Update

You need to assign in the following way

  var product = ProductBL.GetProductById(productId);

        TitleTextBox.Text = product.ProductName;