title: Using a SQL Server Stored Procedure in Visual Studio
body: <p><strong>My Experience &amp; What I'm Using</strong></p>
  
  <p>So I'm just starting off with a very basic web application in ASP.NET to gain a little more familiarity with SQL Server Management Studios and Visual Studios 2010. Normally, I use MySQL, PHP, and Sublime Text Editor 2. I'm not very experienced with C# and implementing a database in Visual Studios. So I'm trying to use a stored procedure from SQL Server Management Studios and implement it in Visual Studios 2010.  </p>
  
  <p><strong>The Issue</strong></p>
  
  <p>So here's my problem: I'm trying to create a basic webpage that links to a SQL Server and be able to add, delete, search and display all records from the database. Now I've written my own code based on what I thought was correct for add/delete and nothing happens when I click the buttons. So I'm sure you can see where my frustration derives from. I'm not sure if the issue is in my C# coding or in my SQL coding. </p>
  
  <p>I'd like to focus on just getting my add/delete buttons to work and then to figure out the logic to display all files. I'd like to be able to click a button and then have it show all files instead of just displaying a grid. My database is called FirstApp.</p>
  
  <p><strong>Here's what's in my web.config file:</strong>     </p>
  
  <pre><code>&lt;add name="FirstApp" connectionString="Data Source=PCNAME\SQLEXPRESS;Initial Catalog=FirstApp;Integrated Security=True"
    providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p><strong>Now this is what's in my Default.aspx.cs file:</strong></p>
  
  <p>*<strong><em>CORRECT CODE NOW!</em>*</strong> </p>
  
  <pre><code>  namespace FirstApp
    {
    public partial class _Default : System.Web.UI.Page
    {
    public string CommandArgument { get; set; }
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      private void MessageBox(string msg)
      {
          Label lbl = new Label();
          lbl.Text = "&lt;script language='javascript'&gt;" + Environment.NewLine +  "window.alert('" + msg + "')&lt;/script&gt;";
          Page.Controls.Add(lbl);
      }
  
      //Add a new company to the database
      protected void add_Click(object sender, EventArgs e)
      {
          SqlDataReader rdr = null;
          string connectionString = null;
          SqlConnection cnn;
          connectionString = "Data Source=ITXDK29M91\\SQLEXPRESS;Initial Catalog=FirstApp;Integrated Security=True";
          cnn = new SqlConnection(connectionString);
          try
          {
              cnn.Open();
              SqlCommand cmd = new SqlCommand("dbo.Add_Company", cnn);
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.AddWithValue("@companyname", companyname.Text);
              cmd.Parameters.AddWithValue("@companyphone", companyphone.Text);
              cmd.Parameters.AddWithValue("@companyid", companyid.Text);
              cmd.Parameters.AddWithValue("@companytype", companytype.Text);
              rdr = cmd.ExecuteReader();
           }
          finally
          {
  
              //Close the connections 
              if (cnn != null)
              {
                  cnn.Close();
              }
              if (rdr != null)
              {
                  rdr.Close();
              }
          }
       }
  
       //Delete a company from the database
       protected void delete_Click(object sender, EventArgs e)
       {
           SqlDataReader rdr = null;
          SqlConnection cnn;
          string connectionString = null;
          connectionString = "Data Source=ITXDK29M91\\SQLEXPRESS;Initial Catalog=FirstApp;Integrated Security=True";
          cnn = new SqlConnection(connectionString);
          try
          {
              cnn.Open();
              SqlCommand cmd = new SqlCommand("dbo.deleteCo", cnn);
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.AddWithValue("@ID", SqlDbType.Int);
              rdr = cmd.ExecuteReader();
  
          }
  
          finally
          {
  
              //Close the connections 
              if (cnn != null)
              {
                  cnn.Close();
              }
              if (rdr != null)
              {
                  rdr.Close();
              }
          }
      }
  
  
      protected void Search_Click(object sender, EventArgs e)
      {
  
      }
  
      protected void Getall_Click(object sender, EventArgs e)
      {
  
      }
  
    }
    }
  </code></pre>
  
  <p><strong>This is what's in my Source Code in Default.aspx</strong></p>
  
  <pre><code> &lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt;      &lt;h2&gt;Ready for an Adventure? Let&amp;#39;s get started! 
       &lt;/h2&gt; &lt;hr /&gt;This is where you can enter information about your company. 
   &lt;br /&gt;
   &lt;form method="post" action=""&gt;
  Company Name:&lt;br /&gt; 
  &lt;asp:TextBox ID="companyname" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;br /&gt;
  Company Phone Number:&lt;br /&gt;
  &lt;asp:TextBox ID="companyphone" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;br /&gt;
  Company Tax ID Number:
  &lt;br /&gt;
  &lt;asp:TextBox ID="companyid" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;br /&gt;
  Type of business: &lt;br /&gt;
       &lt;asp:TextBox ID="companytype" runat="server"&gt;&lt;/asp:TextBox&gt;
  &lt;br /&gt;
   &lt;asp:Button ID="add" runat="server" BackColor="DeepSkyBlue" 
  BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
  CssClass="submitButton" Font-Names="Palatino Linotype" ForeColor="White" 
  onclick="add_Click" Text="Submit" Width="128px" /&gt;
  &lt;/form&gt; &lt;hr /&gt; 
  Want to delete your company information?&lt;br /&gt;
  Enter in the Company ID Number:&amp;nbsp;
  &lt;br /&gt;
   &lt;asp:TextBox ID="PrimaryKey" runat="server" Width="120px"&gt;&lt;/asp:TextBox&gt;
   &lt;br /&gt;
   &lt;asp:Button ID="delete" runat="server" BackColor="DeepSkyBlue" 
  BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
  CssClass="submitButton" Font-Names="Palatino Linotype" ForeColor="White" 
  onclick="delete_Click" Text="Delete Info" Width="119px" /&gt;
  &lt;br /&gt;
  &lt;hr /&gt; 
  Looking for similar companies?
  &lt;br /&gt;
  (Ex: Retail, Designer, Restaurant, etc.) &amp;nbsp;
  &lt;br /&gt;
  Enter the type of company:
   &lt;br /&gt;
   &lt;asp:TextBox ID="scompanyid" runat="server" Width="120px"&gt;&lt;/asp:TextBox&gt;
   &lt;br /&gt;
   &lt;asp:Button ID="Search" runat="server" BackColor="DeepSkyBlue" 
   BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
   CssClass="submitButton" Font-Names="Palatino Linotype" ForeColor="White" 
   onclick="Search_Click" Text="Start Searching!" Width="119px" /&gt;
   &lt;br /&gt;
   &lt;hr /&gt; 
     Want to see all the companies that we work with? &lt;br /&gt; 
     Click the button below! 
   &lt;br /&gt;
   &lt;asp:Button ID="Getall" runat="server" BackColor="DeepSkyBlue" 
   BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
   CssClass="submitButton" Font-Names="Palatino Linotype" ForeColor="White" 
   onclick="Getall_Click" Text="Get all records!" Width="119px" /&gt;
   &lt;br /&gt;
     &lt;br /&gt;
     &lt;/asp:Content&gt;
  </code></pre>
  
  <p><em><strong>UPDATE: I've updated the code to display the correct code. The add button works but my delete button is not. I'm still trying to figure that one out.</em></strong></p>