title: Call a sql server stored procedure when submit button is clicked In asp.net
body: <p>I have following code and now I want to call a stored procedure of SQL, when submit button is clicked.</p>
  
  <p>Can anyone help me where I should place the code that calls a SP when submit button is clicked?</p>
  
  <p>Default.aspx</p>
  
  <pre><code>  &lt;%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  
  &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 id="Head1" runat="server"&gt;
  &lt;title&gt;Login Form&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
  &lt;table align="center"&gt;
  
  &lt;strong&gt;Login Form&lt;/strong&gt;
  
  &lt;tr&gt;
  &lt;td&gt;
  Username:
  &lt;/td&gt;
  &lt;td&gt;
  &lt;asp:TextBox ID="txtUserName" runat="server"/&gt;
  &lt;asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" /&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;
  Password:
  &lt;/td&gt;
  &lt;td&gt;
  &lt;asp:TextBox ID="txtPWD" runat="server" TextMode="Password"/&gt;
  &lt;asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password"/&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Default.aspx.cs</p>
  
  <pre><code>using System;
  using System.Data;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Linq;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Xml.Linq;
  using System.Data;
  
  
  public partial class _Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
          SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
          con.Open();
          SqlCommand cmd = new SqlCommand("select * from Log_Users where UserName =@username and Password=@password", con);
          cmd.Parameters.AddWithValue("@username", txtUserName.Text);
          cmd.Parameters.AddWithValue("@password", txtPWD.Text);
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          da.Fill(dt);
          if (dt.Rows.Count &gt; 0)
          {
              Response.Redirect("Details.aspx");
          }
          else
          {
              ClientScript.RegisterStartupScript(Page.GetType(), "validation", "&lt;script language='javascript'&gt;alert('Invalid Username Or Password')&lt;/script&gt;");
          }
      }
  }
  </code></pre>
  
  <p>Details.aspx</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="Details" %&gt;
  
  &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 id="Head1" runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
      &lt;table align="center"&gt;
          &lt;tr&gt;
      &lt;td&gt;
        &lt;asp:Label ID="lblDisplay" runat="server" Text="Welcome to our sample Site" Font-Bold="true"&gt;&lt;/asp:Label&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
      &lt;/table&gt;
  
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Details.aspx.cs</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Configuration;
  using System.Data;
  using System.Linq;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Xml.Linq;
  
  public partial class Details : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  }
  </code></pre>