title: How To Validate User Inputted Values In DataGridView With Data In SQL table?
body: <p>I'm newbie in C# and Grid, 
                 I have a table in SQL which contains Two columns, Particulars and Price and few values on each side. On other hand on C# win.form grid,there are two columns Particulars and grid. When user inputs any data in the column1(particulars) and if the inputted data does not match with value in Table Particular's Value It should throw an Exception. 
  For this I used CellEndEdit event, but after user enters the data in empty cell how do I check whether the data inputted is correct or not in accordance with DB table values. </p>
  
  <p>I have successfully connected the form with DB and I successfully did this with data settings option of VS But do not know how to do it with SQL database , I have tried but I am confused on validating with SQL database .Here is my code:</p>
  
  <pre><code>namespace Small_Billing_System
  {
  public partial class hitbill : Form
  {
      SqlConnection cn = new SqlConnection(@"server=people-pc\sqlexpress;integrated security=true;database=nec");
      SqlCommand cm = new SqlCommand();
      SqlDataAdapter da = new SqlDataAdapter();
      //da1, da2;
      // ds1, ds2;
      DataSet ds = new DataSet();
      SqlCommandBuilder cb = new SqlCommandBuilder();
      public hitbill()
      {
          InitializeComponent();
      }
  
  
      private void control()
      {
          //dataGridView_2.DataSource = ds;
          //dataGridView_2.DataMember = "tblfood";
          //totalbox.DataBindings.Add("Text", ds, "tblfood.Price");
      }
  
  
  
      private void hitbill_Load(object sender, EventArgs e)
      {
          SqlConnection cn = new SqlConnection(@"server=people-pc\sqlexpress;integrated security=true;database=nec");
          try
          {
              cn.Open();
              MessageBox.Show("Data base connected");
          }
          catch (Exception)
          {
              MessageBox.Show("Data base connection failed");
              throw;
          }
  
          cn.Open();
          cm = new SqlCommand("select * from tblfood", cn);
          da = new SqlDataAdapter(cm);
          da.Fill(ds, "tblfood");
  
  
  
      }
  
      private void dataGridView_2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
      {
          DataGridViewCell currCell = dataGridView_2.CurrentCell;
          string currCellContent = currCell.Value.ToString();
  
          //    SqlCommand cmd = new SqlCommand("select * FROM tblfood");
  
          //check whether inputted values are in DB or not
  
          if (dataGridView_2.CurrentCell.ColumnIndex == 0)
          {
  
              //        if (!((currCellContent == ???????????????.
              //        if (!((currCellContent == "Beans") || (currCellContent == "Juice"))) //cannot do this because there might be many particulars not just 2 or 3
  //And when there are many particulars every time when user inputs value it should be   checked with database and throw an exception/error in case of wrong input.
              //        {
              //            MessageBox.Show("Paticulars not found");
              //            dataGridView_2.CurrentCell.Value = "";
              //        }
              //    }
              //    else if (dataGridView_2.CurrentCell.ColumnIndex ==1)
              //    {
              //         double R = 0.00;
              //        string particular =  dataGridView_2.CurrentRow.Cells[0].Value.ToString().Trim();
              //        if (particular == "Beans")
              //        {
              //            R = Double.Parse(currCellContent) * Properties.Data1.Default.Apple;
              //        }
              //        else if (particular == "Juice")
              //        {
              //            R = Double.Parse(currCellContent) * Properties.Data1.Default.Orange;
              //        }
              //        else
              //        {
              //            R = 0.00;
              //        }
              //        dataGridView_2.CurrentRow.Cells[2].Value = R.ToString();
  
              //        DataGridViewRowCollection rows = dataGridView_2.Rows;
              //        double total = 0.00;
              //        foreach (DataGridViewRow row in rows)
              //        {
              //            if (!row.IsNewRow)
              //            {
              //                double price = Double.Parse(row.Cells[2].Value.ToString());
              //                total = total + price;
              //            }
              //        }
              //        totalbox.Text = total.ToString();
              //    }
              //}
          }
      }
  }
  } 
  </code></pre>