Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am inserting the record in grid view. when VendorFName, VendorLName,VendorCity,VendorState,VendorCountry,VendorDescription all the datas are recorded.

then i am inserting the second record suppose when i repeat the VendorFName and press add new button i want to show the message VendorFName is already exists please enter different name.

for that how to write the code in asp.net with C#.

i send my code as follows;
from my code how to write the code for vendrofname already exists.
C#
protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtFName = (TextBox)ResultGridView.FooterRow.FindControl("txtFName1");
            TextBox txtLName = (TextBox)ResultGridView.FooterRow.FindControl("txtLName1");
            TextBox txtCity = (TextBox)ResultGridView.FooterRow.FindControl("txtCity1");
            TextBox txtState = (TextBox)ResultGridView.FooterRow.FindControl("txtState1");
            TextBox txtCountry = (TextBox)ResultGridView.FooterRow.FindControl("txtCountry1");
            TextBox txtDescription = (TextBox)ResultGridView.FooterRow.FindControl("txtDescription1");
            SqlConnection conn = new SqlConnection("Server=(local);initial catalog=master;Trusted_Connection=True");
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO Vendors(VendorFName, VendorLName,VendorCity,VendorState,VendorCountry,VendorDescription) Values('" + txtFName.Text + "', '" + txtLName.Text + "', '" + txtCity.Text + "', '" + txtState.Text + "', '" + txtCountry.Text + "' , '" + txtDescription.Text + "')";
                conn.Open();
                cmd.ExecuteNonQuery();
                FillVendorGrid();
                conn.Close();
            
        }
    }
Posted
Updated 4-Dec-12 20:36pm
v2

C#
SqlDataReader dr=new SqlDataReader("select * from table where VendorFName ='"+ txtFName.Text+"'",conn);
if(dr.read())
{
dr.Close();
Messagebox.Show("Already exist");
}
else
{
dr.close();
// go for your insert  code.
}
 
Share this answer
 
Hi dear,

check this solution.

protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName.Equals("AddNew"))
  {
     string SQLExists = "select * from table where VendorFName ='"+ txtFName.Text+"'";
     SqlCommand cmd = new SqlCommand (SQLExists,conn ); 
     SqlDataAdapter da = new SqlDataAdapter(cmd ); 
     DataTable dt = new DataTable();
     da.Fill(dt);
     if(dt.Rows.Count > 0)
     {
        Messagebox.Show("Vendor FName Already exist");
        return;
     }

  else
  {
    TextBox txtFName = (TextBox)ResultGridView.FooterRow.FindControl("txtFName1");
    TextBox txtLName = (TextBox)ResultGridView.FooterRow.FindControl("txtLName1");
    TextBox txtCity = (TextBox)ResultGridView.FooterRow.FindControl("txtCity1");
    TextBox txtState = (TextBox)ResultGridView.FooterRow.FindControl("txtState1");
    TextBox txtCountry = (TextBox)ResultGridView.FooterRow.FindControl("txtCountry1");
    TextBox txtDescription =  (TextBox)ResultGridView.FooterRow.FindControl("txtDescription1");
    SqlConnection conn = new SqlConnection("Server=(local);initial catalog=master;   Trusted_Connection=True");
    cmd.Connection = conn;
   cmd.CommandText = "INSERT INTO Vendors(VendorFName,   VendorLName, VendorCity, VendorState, VendorCountry, VendorDescription)     Values('" + txtFName.Text + "', '" + txtLName.Text + "', '" + txtCity.Text + "', '" + txtState.Text + "', '" + txtCountry.Text + "' , '" + txtDescription.Text + "')";
    conn.Open();
    cmd.ExecuteNonQuery();
    FillVendorGrid();
    conn.Close();

  }
} 
 
Share this answer
 
v2
you can use linq

C#
int rowIndex = -1;

        DataGridViewRow row = dgv.Rows
            .Cast<DataGridViewRow>()
            .Where(r =>r.Cells["SystemId"].Value.ToString().Equals(searchValue))
            .First();

        rowIndex = row.Index;

if(rowindex >1 )
   Message.Box("Message");


Or
C#
foreach(DataGridViewRow dgvRow in ResultGridView.Rows)
{
     if(row.Cells["columnname"].Value.ToString().Equals(searchValue))
     {
            Message.Box("Message");
     }
} 
 
Share this answer
 

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