title: Want to show data in gridview when click on button
body: <p>I have  two textboxes, one dropdown and two button's. When I enter value in textboxes, select value from dropdown and press <code>button1</code>, my data is stored in the database and I can view that in my grid when I click on <code>button2</code>. </p>
  
  <p>But the problem is when I press <code>button2</code> without entering any details it should show validation error which I add on my textboxes and dropdown.</p>
  
  <p><strong>code for aspx.cs</strong>:</p>
  
  <pre><code>protected void Button1_Click(object sender, EventArgs e)
  {
      SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=EMS;Integrated Security=True");
      con.Open();
  
      string UserName = TextBox1.Text;
      string Password = TextBox2.Text;
      string Role = DropDownList1.Text;
  
      using (SqlCommand cmd = con.CreateCommand())
      {
          cmd.CommandText = "insert into Login(UserName,Password,Role) values('" + UserName + "','" + Password + "','" + Role + "')";
          cmd.Parameters.AddWithValue("@UserName", TextBox1.Text);
          cmd.Parameters.AddWithValue("@Password", TextBox2.Text);
          cmd.Parameters.AddWithValue("@Role", DropDownList1.Text);
          cmd.ExecuteNonQuery();
      }
  
      con.Close();
      TextBox1.Text = string.Empty;
      TextBox2.Text = string.Empty;
      DropDownList1.Text = string.Empty;
      TextBox1.Focus();
  } 
  protected void Button2_Click1(object sender, EventArgs e)
  {
      SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=EMS;Integrated Security=True");
      con.Open();
  
      using (SqlCommand cmd = con.CreateCommand())
      {
          SqlCommand com = new SqlCommand("Select * from Login", con);
          SqlDataAdapter sda = new SqlDataAdapter(com);
  
          DataSet ds = new DataSet();
          sda.Fill(ds);
  
          GridView1.DataSource = ds;
          GridView1.DataBind();
      }
      con.Close();
  }
  </code></pre>
  
  <p><strong>code for aspx</strong>:</p>
  
  
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;style type="text/css"&gt;
      .style1
      {
          width: 100%;
          height: 178px;
      }
      .style3
      {
          width: 87px;
      }
      .style4
      {
          width: 87px;
          height: 110px;
      }
      .style5
      {
          height: 110px;
      }
  &lt;/style&gt;
   &lt;/head&gt;
   &lt;body style="height: 387px"&gt;
  &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
              &lt;tr&gt;
              &lt;td class="style3"&gt;
            &amp;nbsp;User name&lt;/td&gt;
              &lt;td&gt;
           &lt;asp:TextBox ID="TextBox1" runat="server" style="margin-left: 0px"&gt; &lt;/asp:TextBox&gt;
                &amp;nbsp;&amp;nbsp;
                  &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                      ControlToValidate="TextBox1" ErrorMessage="Enter User Name" Font- Bold="True"&gt;*&lt;/asp:RequiredFieldValidator&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
                &lt;tr&gt;
              &lt;td class="style3"&gt;
                  Password&lt;/td&gt;
              &lt;td&gt;
      &lt;asp:TextBox ID="TextBox2" runat="server" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
      &amp;nbsp;&amp;nbsp;
                  &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                      ControlToValidate="TextBox2" ErrorMessage="Enter Password" Font- Bold="True"&gt;*&lt;/asp:RequiredFieldValidator&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="style3"&gt;
                  Select Role&lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:DropDownList ID="DropDownList1" runat="server"&gt;
                      &lt;asp:ListItem&gt;Select One&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Admin&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Manager&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;User&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;&lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator3"  runat="server" 
                      ControlToValidate="DropDownList1" ErrorMessage="Select Role"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                  &amp;nbsp;&lt;/td&gt;
              &lt;/tr&gt;
               &lt;tr&gt;
              &lt;td class="style4"&gt;
                  &lt;/td&gt;
              &lt;td class="style5"&gt;
      &lt;asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Sumit" /&gt;
  
      &lt;asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="65px" /&gt;
  
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;/table&gt;
  
  &lt;/div&gt;
  &lt;p&gt;
                               &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &lt;asp:Button ID="Button2" runat="server" onclick="Button2_Click1" Text="Show" /&gt;
                  &amp;nbsp;&lt;/p&gt;
  &lt;p&gt;
  </code></pre>
  
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                      
                          
                          
                          
                          
                          
                          
                          
                          
                          
                          
                      
          &nbsp;</p>
      
  
  </p>