title: StoredProcedure in C#, passing parameters from text boxes
body: <p>I have very simple page just wan to find out how to pass data from text boxes into my procedure. I am kind of new with passing parameters from code behind in to DB,I have one page  for that code is </p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
  
          &lt;asp:TextBox ID="LastName" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:TextBox ID="FirstName" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:TextBox ID="Phone1" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:TextBox ID="Phone2" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
          &lt;asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /&gt;
          &lt;br /&gt;
          &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:TicketsConnectionString %&gt;" SelectCommand="SELECT * FROM [Employee]"&gt;&lt;/asp:SqlDataSource&gt;
  
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and aspx code</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Data.SqlClient;
  using System.Configuration;
  
  public partial class _Default : System.Web.UI.Page
  {
  
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      string connectionString = ConfigurationManager.ConnectionStrings["TicketsConnectionString"].ConnectionString;
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          DataSet ds = new DataSet();
      SqlConnection con = new SqlConnection(connectionString);
      SqlCommand cmd = new SqlCommand("InsertIntoEmployee", con);
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = LastName.Text;
      cmd.Parameters.Add("@FirstName", SqlDbType.VarChar);
      cmd.Parameters.Add("@Phone1", SqlDbType.VarChar);
      cmd.Parameters.Add("@Phone2", SqlDbType.VarChar);
      cmd.Parameters["@LastName"].Value = LastName.Text;
      cmd.Parameters["@FirstName"].Value = FirstName.Text;
      cmd.Parameters["@Phone1"].Value = Phone1.Text;
      cmd.Parameters["@Phone2"].Value = Phone2.Text;
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
      }
  }
  </code></pre>
  
  <p>web.config looks like this </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!--
    For more information on how to configure your ASP.NET application, please visit
    http://go.microsoft.com/fwlink/?LinkId=169433
    --&gt;
  &lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="TicketsConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=&amp;quot;C:\Users\Asya\Documents\Visual Studio 2012\WebSites\WebSite6\App_Code\Tickets.mdf&amp;quot;;Integrated Security=True;Connect Timeout=30"
        providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.5"/&gt;
      &lt;httpRuntime targetFramework="4.5"/&gt;
    &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>When i pass simple data in to my prosedure it throws error </p>
  
  <pre><code>An SqlParameter with ParameterName '@LastName' is not contained by this SqlParameterCollection.
  </code></pre>
  
  <p>How to resolve this problem? Because on DB  side procedure works perfectly
   As I saed before I am really  new in to it. and I fixed previous problems but  here another one </p>
  
  <pre><code>The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
  
  Source Error: 
  
  
  Line 34:         cmd.Parameters["@Phone2"].Value = Phone2.Text;
  Line 35:         con.Open();
  Line 36:         cmd.ExecuteNonQuery();
  Line 37:         con.Close();
  Line 38: 
  </code></pre>
  
  <p>Procedure which i use</p>
  
  <pre><code>procedure InsertIntoEmployee
  @LastName Nvarchar (25) ,
  @FirstName Nvarchar (25) , 
  @Phone1 Nvarchar (25) ,
  @Phone2 Nvarchar (25)
  as 
  insert into  Employee  (LastName , FirstName,Phone1,Phone2)values (@LastName,@FirstName,@Phone1,@Phone2);
  </code></pre>
  
  <p>when i run prosedure on Db side like 
  <code>EXEC InsertIntoEmployee N'петров', N'петр', 99999999,null</code>
  it works perfectly but from asp.net side its throwing errors </p>