Add data to data gridview

Joined
Feb 6, 2009
Messages
3
Reaction score
0
i have one gridview name dgvShipList

and i have three control above dgvShipList with button 'Add'

i have 2 textboxes for input one for barcode and another one for Qty

another control is dropdown, i retrieve items from my DB

after user fill in the textbox controls and click 'Add'

this btnAdd_Click must insert data into gridview

but now dgvShipList can have only one row always

after i click add button, how can i add next entry to dgvShipList

which way i need to do.

PS. new row that we insert must not duplicate with any rows in dgvShipList



Can anyone help me for this problem?



thanks a lot .
 
Joined
Feb 6, 2009
Messages
3
Reaction score
0
here is my btnAdd_Click code

protected void btnAdd_Click(object sender, EventArgs e)
{

if (txtQty.Text.Trim() == "")
{
Response.Write("<script type='text/javascript'>alert('Please input quantity!!');</script>");
}
else
{
string strConn = "Data Source=MIXAR-PC;Initial Catalog=StockProductInfo;Integrated Security=True";

Conn = new SqlConnection(strConn);
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();

string sqlQty = "SELECT PO_ProdQty ";
sqlQty += "FROM tranProductOrder ";
sqlQty += "WHERE Prod_Barcode = " + txtBarcode.Text;
sqlQty += "AND Order_ID = " + ddlOrderID.SelectedItem.Text;

SqlDataAdapter da2 = new SqlDataAdapter(sqlQty, Conn);
DataSet ds2 = new DataSet();
da2.Fill(ds2, "Qty");

int orderQty = int.Parse(ds2.Tables["Qty"].Rows[0]["PO_ProdQty"].ToString());
int inputQty = int.Parse(txtQty.Text);

if (inputQty > orderQty)
{
Response.Write("<script type='text/javascript'>alert('Your input quantity is too much!!');</script>");
}
else
{
Session["Quantity"] = txtQty.Text;
Response.Write("<script type='text/javascript'>alert('This information will add to shipping list!!');</script>");

sb.Append("SELECT tranProductOrder.Prod_Barcode as ProdBarcode, ");
sb.Append("tranProductOrder.Order_ID as OrderID, ");
sb.Append("masProduct.Prod_Name as ProdName, ");
sb.Append("tabCategory.Category_Name as ProdCategory, ");
sb.Append("tabPackageSize.Pack_Size as ProdPackSize, ");
sb.Append("masStock.Prod_QtyInStock as QtyInStock ");
sb.Append("FROM tranProductOrder, masBarcode, masStock, ");
sb.Append("masProduct, tabCategory, tabPackageSize ");
sb.Append("WHERE tranProductOrder.Prod_Barcode = masBarcode.Prod_Barcode ");
sb.Append("AND masBarcode.Prod_ID = masProduct.Prod_ID ");
sb.Append("AND masProduct.Category_ID = tabCategory.Category_ID ");
sb.Append("AND masBarcode.Prod_Barcode = masStock.Prod_Barcode ");
sb.Append("AND masBarcode.Pack_ID = tabPackageSize.Pack_ID ");
sb.Append("AND tranProductOrder.Prod_Barcode = @Barcode ");
sb.Append("AND tranProductOrder.Order_ID = @OrdID ");

string sqlShip = sb.ToString();
DataTable dtShipList;
SqlCommand com = new SqlCommand();
com.CommandType = CommandType.Text;
com.CommandText = sqlShip;
com.Parameters.Clear();
com.Parameters.Add("@Barcode", SqlDbType.VarChar).Value = txtBarcode.Text;
com.Parameters.Add("@OrdID", SqlDbType.VarChar).Value = ddlOrderID.SelectedItem.Text;

com.Connection = Conn;
SqlDataReader dr;
dr = com.ExecuteReader();
if (dr.HasRows)
{
dtShipList = new DataTable();
dtShipList.Load(dr);
dgvShipList.DataSource = dtShipList;
dgvShipList.DataBind();
dgvShipList.Visible = true;

foreach (GridViewRow r in dgvShipList.Rows)
{
TextBox txtShipQty = (TextBox)r.FindControl("txtQty");
txtShipQty.Text = Session["Quantity"].ToString();
}
txtBarcode.Text = "";
ddlOrderID.Enabled = false;
txtQty.Enabled = false;
btnClear.Enabled = true;
btnSubmit.Enabled = true;
}
dr.Close();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top