title: Exception handling in Entity Framework
body: <p>I have a form that it has some field and has relationship with database. I'm using entity framework I wanna handle exception before sql server send error message. For example when user enter string value in number field win or web application handle exception before sql server handle it. I wrote this code but it don't work for all the exception. For example if field was empty or has invalid type said input string is not in correct format.</p>
  
  <pre><code> using (var context  = new entityTestEntities2())
          {
              try
              {
                  int stNumber = Convert.ToInt32(textBox2.Text);
                  var allCustomers = context.setcust(null, stNumber);
              }
              catch(Exception ex)
              {
                  if (ex.Message.Contains("correct format"))
                  { 
                  int x= System.Runtime.InteropServices.Marshal.GetExceptionCode();
                       MessageBox.Show("error number"+x.ToString()+ex.Message);
                  }
               }
          } 
  </code></pre>