title: try catch does not work in gridview in ASP.net C#
body: <p>I'm trying to define try catch for a gridview in asp.net on C#, I have used several method such as show on the label or java script but they didn't work,
  thanks to help me 
  here is my code </p>
  
  <pre><code>protected void AddNewSP(object sender, EventArgs e)
          {
              try
              { 
                  string Id = ((TextBox)GridView1.FooterRow.FindControl("txtId")).Text;
                  string PartName = ((TextBox)GridView1.FooterRow.FindControl("txtPartName")).Text;
                  string Coverage = ((TextBox)GridView1.FooterRow.FindControl("txtCoverage")).Text;
                  string SupplierName = ((TextBox)GridView1.FooterRow.FindControl("txtSupplierName")).Text;
                  string Status = ((TextBox)GridView1.FooterRow.FindControl("txtStatus")).Text;                           
                      SqlConnection con = new SqlConnection(strConnString);
                      SqlCommand cmd = new SqlCommand();
                      cmd.CommandType = CommandType.Text;
                      cmd.CommandText = "insert into SupplySituation(Id, PartName, Coverage, SupplierName, Status) " +
                      "values(@Id, @PartName, @Coverage, @SupplierName, @Status);" +
                      "select Id, PartName, Coverage, SupplierName, Status from SupplySituation ORDER BY Id DESC";
                      cmd.Parameters.Add("@Id", SqlDbType.VarChar).Value = Id;
                      cmd.Parameters.Add("@PartName", SqlDbType.VarChar).Value = PartName;
                      cmd.Parameters.Add("@Coverage", SqlDbType.VarChar).Value = Coverage;
                      cmd.Parameters.Add("@SupplierName", SqlDbType.VarChar).Value = SupplierName;
                      cmd.Parameters.Add("@Status", SqlDbType.VarChar).Value = Status;
                      GridView1.DataSource = GetData(cmd);
                      GridView1.DataBind();
                  }
                  catch (Exception ex)
                  {
                      ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + ex.Message.ToString() + "');", true);
                  }         
          }
  
  private DataTable GetData(SqlCommand cmd)
          {
  
              DataTable dt = new DataTable();
              SqlConnection con = new SqlConnection(strConnString);
              SqlDataAdapter sda = new SqlDataAdapter();
              cmd.CommandType = CommandType.Text;
              cmd.Connection = con;
              con.Open();
              sda.SelectCommand = cmd;
              sda.Fill(dt);
              return dt;           
          }
  </code></pre>
  
  <p>in fact there is no any error or any action , when I click on the "ADD" button with wrong data nothing happens, I except to show the error but nothing happens </p>
  
  <p>my front code </p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" Width="750px"
                                  AutoGenerateColumns="False" Font-Names="Arial"
                                  Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B"
                                  HeaderStyle-BackColor="green" AllowPaging="True" ShowFooter="True"
                                  OnPageIndexChanging="OnPagingSP" OnRowEditing="EditSP"
                                  OnRowUpdating="UpdateSP" OnRowCancelingEdit="CancelEditSP"&gt;
  
                                  &lt;Columns&gt;
  
                                      &lt;asp:TemplateField HeaderText="Id"&gt;
                                          &lt;ItemTemplate&gt;
  
                                              &lt;asp:Label ID="lblId" runat="server"
                                                  Text='&lt;%# Eval("Id")%&gt;'&gt;&lt;/asp:Label&gt;
  
                                          &lt;/ItemTemplate&gt;
  
                                          &lt;FooterTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtId" Width="20px"
                                                  MaxLength="5" runat="server"&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/FooterTemplate&gt;
                                      &lt;/asp:TemplateField&gt;
  
                                      &lt;asp:TemplateField ItemStyle-Width="50px" HeaderText="PartName"&gt;
  
                                          &lt;ItemTemplate&gt;
  
                                              &lt;asp:Label ID="lblPartName" runat="server"
                                                  Text='&lt;%# Eval("PartName")%&gt;'&gt;&lt;/asp:Label&gt;
  
                                          &lt;/ItemTemplate&gt;
                                          &lt;EditItemTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtPartName" runat="server"
                                                  Text='&lt;%# Eval("PartName")%&gt;'&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/EditItemTemplate&gt;
                                          &lt;FooterTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtPartName" Width="100px"
                                                  MaxLength="20" runat="server"&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/FooterTemplate&gt;
  
                                          &lt;ItemStyle Width="30px" /&gt;
  
                                      &lt;/asp:TemplateField&gt;
  
                                      &lt;asp:TemplateField ItemStyle-Width="100px" HeaderText="Coverage"&gt;
  
                                          &lt;ItemTemplate&gt;
  
                                              &lt;asp:Label ID="lblCoverage" runat="server"
                                                  Text='&lt;%# Eval("Coverage")%&gt;'&gt;&lt;/asp:Label&gt;
  
                                          &lt;/ItemTemplate&gt;
  
                                          &lt;EditItemTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtCoverage" runat="server"
                                                  Text='&lt;%# Eval("Coverage")%&gt;'&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/EditItemTemplate&gt;
  
                                          &lt;FooterTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtCoverage" runat="server"&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/FooterTemplate&gt;
  
                                          &lt;ItemStyle Width="100px" /&gt;
  
                                      &lt;/asp:TemplateField&gt;
  
                                      &lt;asp:TemplateField ItemStyle-Width="150px" HeaderText="SupplierName"&gt;
  
                                          &lt;ItemTemplate&gt;
  
                                              &lt;asp:Label ID="lblSupplierName" runat="server"
                                                  Text='&lt;%# Eval("SupplierName")%&gt;'&gt;&lt;/asp:Label&gt;
  
                                          &lt;/ItemTemplate&gt;
  
                                          &lt;EditItemTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtSupplierName" runat="server"
                                                  Text='&lt;%# Eval("SupplierName")%&gt;'&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/EditItemTemplate&gt;
  
                                          &lt;FooterTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtSupplierName" runat="server"&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/FooterTemplate&gt;
  
                                          &lt;ItemStyle Width="150px" /&gt;
  
                                      &lt;/asp:TemplateField&gt;
  
                                      &lt;asp:TemplateField ItemStyle-Width="150px" HeaderText="Status"&gt;
  
                                          &lt;ItemTemplate&gt;
  
                                              &lt;asp:Label ID="lblStatus" runat="server"
                                                  Text='&lt;%# Eval("Status")%&gt;'&gt;&lt;/asp:Label&gt;
  
                                          &lt;/ItemTemplate&gt;
  
                                          &lt;EditItemTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtStatus" runat="server"
                                                  Text='&lt;%# Eval("Status")%&gt;'&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/EditItemTemplate&gt;
  
                                          &lt;FooterTemplate&gt;
  
                                              &lt;asp:TextBox ID="txtStatus" runat="server"&gt;&lt;/asp:TextBox&gt;
  
                                          &lt;/FooterTemplate&gt;
  
                                          &lt;ItemStyle Width="150px" /&gt;
  
                                      &lt;/asp:TemplateField&gt;
  
                                      &lt;asp:TemplateField&gt;
  
                                          &lt;ItemTemplate&gt;
  
                                              &lt;asp:LinkButton ID="lnkRemove" runat="server"
                                                  CommandArgument='&lt;%# Eval("Id")%&gt;'
                                                  OnClientClick="return confirm('Do you want to delete?')"
                                                  Text="Delete" OnClick="DeleteSP"&gt;&lt;/asp:LinkButton&gt;
  
                                          &lt;/ItemTemplate&gt;
  
                                          &lt;FooterTemplate&gt;
  
                                              &lt;asp:Button ID="btnAddNewSP" runat="server" Text="Add"
                                                  OnClick="AddNewSP" /&gt;
  
                                          &lt;/FooterTemplate&gt;
  
                                      &lt;/asp:TemplateField&gt;
  
                                      &lt;asp:CommandField ShowEditButton="True" /&gt;
  
                                  &lt;/Columns&gt;
  
                                  &lt;AlternatingRowStyle BackColor="#C2D69B" /&gt;
  
                                  &lt;HeaderStyle BackColor="Green" /&gt;
  
                              &lt;/asp:GridView&gt;
  </code></pre>