Step 1: Create Stored Procedure, see below code and modify according to your need.
CREATE PROCEDURE [dbo].[InsertUser] (
@User varchar(50),
@Pass varchar(50)
) AS
INSERT INTO Users VALUES(@Username, @Password)
Step 2: Please see below code and explaination for each line.
Complete Code
string user = ... // get username from user
string pass = ... // get password from user
SqlConnection conn = new SqlConnection("Data
Source=localhost;Database=MyDB;Integrated Security=SSPI");
SqlCommand command = new SqlCommand("InsertUser", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@User", SqlDbType.VarChar).Value = user;
command.Parameters.Add("@Pass", SqlDbType.VarChar).Value = pass;
conn.Open();
int rows = command.ExecuteNonQuery();
conn.Close();
Explanation of above code line by line
Assign your text box value to this variables
string user = ... // get username from user
string pass = ... // get password from user
Make sqlconnection using your connectionstring
SqlConnection conn = new SqlConnection("Data
Source=localhost;Database=MyDB;Integrated Security=SSPI");
Declare sqlcommand and pass your Stored Procedure name and make connection to it.
All pass all the parameter to Stored Procudure.
SqlCommand command = new SqlCommand("InsertUser", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@User", SqlDbType.VarChar).Value = user;
command.Parameters.Add("@Pass", SqlDbType.VarChar).Value = pass;
Open connection to database and run your store procedure, thus you can insert your form data to database succussfully.
conn.Open();
int rows = command.ExecuteNonQuery();
conn.Close();
Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com
No comments :
Post a Comment