Skip to content

Using store Procedure how to save the record in database in aso.net charp code.

narasiman rao

Asked Aug 27, 2012, 9:33 PM EDT1 answer
Share
LinkedInX
 I have Designing as follows


 Employee ID:                      Dropdownlist
 Employee Name:                 textbox1
 Date of birth                        textbox2
 Age                                      textbox3
 Address                               textbox4
 Area                                     textbox5
 Mobile No                             textbox6


for that how to save the record using strore procdedure please help me and send the code.


Thanks,
Rao.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Akkiraju Ivaturi
    0
    Create a stored procedure in sql server:
    Create Procedure SaveEmployee
    @EmployeeID Int,
    @EmployeeName Varchar(50),
    @DOB DateTime,
    @Age int,
    @Address Varchar(50),
    @Area Varchar(50),
    @Mobile Varchar(50)
    AS
    BEGIN
    INSERT INTO TABLE1 VALUES( @EmployeeID , @EmployeeName , @DOB ... All the parameters as comma seperated)
    END

    If EmployeeID is Identity column, then remove the parameter @EmployeeID


    Java
     SqlConnection conn = null;
    
    conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
    conn.Open();
    
    // 1.  create a command object identifying
    //     the stored procedure
    SqlCommand cmd  = new SqlCommand("SaveEmployee ", conn);
    
    // 2. set the command object so it knows
    //    to execute a stored procedure
    cmd.CommandType = CommandType.StoredProcedure;
    
    // 3. add parameter to command, which
    //    will be passed to the stored procedure
    cmd.Parameters.Add(new SqlParameter("@EmployeeID", dropdownlist.selectedvalue));
    
     cmd.Parameters.Add(new SqlParameter("@EmployeeName", TextBox1.Text));
    
     cmd.Parameters.Add(new SqlParameter("@DOB", TextBox2.Text));
    ...Likewise for all parameters add
    // execute the command
    cmd.ExecuteNonQuery();
    0