Click here to Skip to main content
15,882,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello developers,
I want to compare textbox value with data table created in the session. If the textbox value matches with data table column item id, it should not allow to re enter that item on button click. Screenshot

What I have tried:

protected void AddInGrid(object sender, EventArgs e)
{

messagelabel.Text = "";
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTabele"];
DataRow drCurrentRow = null;

if (dtCurrentTable.Rows.Count > 0 & txtquantity.Text != "0" & checkstock()==true)
{

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{

//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Item Id"] = Convert.ToInt32(txtid.Text);
drCurrentRow["Item Name"] = txtname.Text;
drCurrentRow["Price"] = Convert.ToInt32(txtprice.Text);
drCurrentRow["Quantity"] = Convert.ToInt32(txtquantity.Text);
drCurrentRow["Item Total"] = Convert.ToInt32(txttotalitemprice.Text);
}
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();

}


//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["CurrentTabele"] = dtCurrentTable;
//binding Gridview with New Row
gvGridview1.DataSource = dtCurrentTable;
gvGridview1.DataBind();

}


else if (checkstock() == false)
{
messagelabel.Text = "Stock Not Enough";
}



}
Posted
Updated 13-Jul-17 7:10am

1 solution

Hi,

Add the code below at the beginning of your function,

C#
//try to find the item id
DataRow[] drs = dtCurrentTable.Select("Item Id = '" + yourtextbox.Text + "'");

//Verify if the select return one line 
if (drs.Length > 0)
{
    //item finded, exit without add line
    messagelabel.Text = "This data already exists. You can't add again.";
    return;                    
}


Here! The complete code.
C#
protected void AddInGrid(object sender, EventArgs e)
{

messagelabel.Text = "";
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTabele"];

/****************************************************************
* CODE ADDED                                                    *
****************************************************************/

//try to find the item id
DataRow[] drs = dtCurrentTable.Select("Item Id = '" + yourtextbox.Text + "'");

//Verify if the select return one line 
if (drs.Length > 0)
{
    //item finded, exit without add line
    messagelabel.Text = "This data already exists. You can't add again.";
    return;                    
}

/****************************************************************
* END CODE                                                      *
****************************************************************/

//Your code

DataRow drCurrentRow = null;

if (dtCurrentTable.Rows.Count > 0 & txtquantity.Text != "0" & checkstock()==true)
{

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{

//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Item Id"] = Convert.ToInt32(txtid.Text);
drCurrentRow["Item Name"] = txtname.Text;
drCurrentRow["Price"] = Convert.ToInt32(txtprice.Text);
drCurrentRow["Quantity"] = Convert.ToInt32(txtquantity.Text);
drCurrentRow["Item Total"] = Convert.ToInt32(txttotalitemprice.Text);
}
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();

}


//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["CurrentTabele"] = dtCurrentTable;
//binding Gridview with New Row
gvGridview1.DataSource = dtCurrentTable;
gvGridview1.DataBind();

}


else if (checkstock() == false)
{
messagelabel.Text = "Stock Not Enough";
}

}
 
Share this answer
 
v3

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