Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a form on a webpage that i want to use a stored procedure to input details into a database. Im not sure why it will not work.

My mark-up:
C#
<p>Here you can edit your personal details.</p><br />
   <asp:Label ID="lblPersonalDetails" runat="server">
           </asp:Label><br /><br />
   <asp:TextBox ID="txtCompanyName" runat="server" CssClass="PersonalDetailForm" ></asp:TextBox> <asp:Label ID="lblCompanyName" runat="server" Text="Enter your company name"></asp:Label><br />
   <asp:TextBox ID="txtFirstName" runat="server" CssClass="PersonalDetailForm" ></asp:TextBox> <asp:Label ID="lblFirstName" runat="server" Text="Enter your first name"></asp:Label><br />
   <asp:TextBox ID="txtLastName" runat="server" CssClass="PersonalDetailForm" ></asp:TextBox> <asp:Label ID="lblLastName" runat="server" Text="Enter your last name"></asp:Label><br />
   <asp:Button ID="PersonalDetailsSubmit" runat="server" CssClass="PersonalDetailForm" Text="Submit" OnClick="PersonalDetailsSubmit_Click" />


My Code-behind:

C#
public partial class restrictedAccess_PersonalDetails : System.Web.UI.Page
{
    
       private string connectionString =
        WebConfigurationManager.ConnectionStrings["BillyNicClothingCon"].ConnectionString;
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void PersonalDetailsSubmit_Click(object sender, EventArgs e)
    {
     
            
  
            MembershipUser user = Membership.GetUser();

            string bncUserID = user.ProviderUserKey.ToString();

           
        SqlConnection con = new SqlConnection(connectionString);

        SqlCommand cmd = new SqlCommand("EXECUTE CustomerDetails.bnc_insCustomer", con);

        cmd.CommandType = CommandType.StoredProcedure;

        // Add the paramaters
        cmd.Parameters.Add(new SqlParameter("@MemId", SqlDbType.UniqueIdentifier));
        cmd.Parameters.Add(new SqlParameter("@CompanyName", SqlDbType.VarChar, 100));
        cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar, 50));
        cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar, 50));
        
        // Add parameter values
        cmd.Parameters[0].Value = bncUserID;
        cmd.Parameters[1].Value = txtCompanyName.Text;
        cmd.Parameters[2].Value = txtFirstName.Text;
        cmd.Parameters[3].Value = txtLastName.Text;
         

        // Try to open the database and execute query

 try { 

       con.Open();
       int i = cmd.ExecuteNonQuery();
         lblPersonalDetails.Text = "Details entered successfully";
         txtCompanyName.Text = "";
         txtFirstName.Text = "";
         txtLastName.Text = "";

       
}
        catch {
            lblPersonalDetails.Text = "Error inserting details";
        }
        finally {
     con.Close();
        }
        }}

My stored procedure:

SQL
CREATE PROCEDURE [CustomerDetails].[bnc_insCustomer] 
	-- Add the parameters for the stored procedure here
	@MemId uniqueidentifier, 
	@CompanyName varchar(100), 
	@FirstName varchar(50),
	@LastName varchar(50)
	
AS
BEGIN
	INSERT INTO CustomerDetails.Customers (MembershipId, CompanyName, FirstName, LastName)
	VALUES (@MemId, @CompanyName, @FirstName, @LastName)
	
END


GO


As i said i am not sure what is going wrong and any advice anyone can offer will be greatly appreciated.
Posted
Comments
NaibedyaKar 17-Oct-14 8:27am    
Can you tell us what error are you getting and where

pass parameter values like this................
C#
cmd.Parameters.Add(new SqlParameter("@MemId",bncUserID));
       cmd.Parameters.Add(new SqlParameter("@CompanyName", txtCompanyName.Text));
       cmd.Parameters.Add(new SqlParameter("@FirstName",txtFirstName.Text));
       cmd.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
 
Share this answer
 
v2
Hi Deue Make it ypur code as much as simple .........
int flag = 0;
          try
          {
              Sqlconnector sc = new Sqlconnector();
              SqlCommand cmd;
              sc.connect();
              cmd = new SqlCommand("bnc_insCustomer", sc.con);
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.Add(new SqlParameter("@MemId",bncUserID));
              cmd.Parameters.Add(new SqlParameter("@CompanyName", txtCompanyName.Text));
              cmd.Parameters.Add(new SqlParameter("@FirstName",txtFirstName.Text));
              cmd.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
              flag = int.Parse(cmd.ExecuteNonQuery().ToString());
              sc.disconnect();
          }
          catch
          {

          }
          return flag;
 
Share this answer
 
Comments
Member 11174012 23-Oct-14 3:19am    
simple solution....Good

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900