How to Pass Sql Parameters in SqlServer througn asp.net


Sometimes we need to execute storedprocedures in our asp.net

if we want to pass any parameters the following things will help..

Type1:

SqlConnection con =

new SqlConnection("Data Source=SERVER2;Initial Catalog=IC;User ID=sa;Password=123");

SqlCommand cmd = new SqlCommand("sp_Register",con);

cmd.Parameters.AddWithValue("@fname", TextBox1.Text);

cmd.Parameters.AddWithValue("@lname", TextBox2.Text);

cmd.Parameters.AddWithValue("@address", TextBox3.Text);

cmd.Parameters.AddWithValue("@uname", TextBox4.Text);

cmd.Parameters.AddWithValue("@pwd", TextBox5.Text);

cmd.Parameters.AddWithValue("@mail", TextBox6.Text);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

Type2:

SqlParameter[] p = new SqlParameter[9];

p[0] = new SqlParameter("@username", TextBox1.Text);

p[1] = new SqlParameter("@firstname", TextBox2.Text);

p[2] = new SqlParameter("@lastName", TextBox3.Text);

p[3] = new SqlParameter("@state", TextBox4.Text);

p[4] = new SqlParameter("@city", TextBox5.Text);

p[5] = new SqlParameter("@location", TextBox6.Text);

p[6] = new SqlParameter("@mobile", TextBox7.Text);

p[7] = new SqlParameter("@zipcode", TextBox8.Text);

p[8] = new SqlParameter("@emailid", TextBox9.Text);

Type3:

string _sql = "Sp_UserRegistration";

SqlParameter[] Param = new SqlParameter[6];

Param[0] = new SqlParameter("@fname", SqlDbType.VarChar, 200);

Param[0].Value = TextBox1.Text;

Param[1] = new SqlParameter("@lname", SqlDbType.VarChar, 200);

Param[1].Value = TextBox2.Text;

Param[2] = new SqlParameter("@address", SqlDbType.NVarChar);

Param[2].Value = TextBox3.Text;

Param[3] = new SqlParameter("@uname", SqlDbType.NVarChar);

Param[3].Value = TextBox4.Text;

Param[4] = new SqlParameter("@pwd", SqlDbType.NVarChar);

Param[4].Value = TextBox5.Text;

Param[5] = new SqlParameter("@mail", SqlDbType.VarChar, 100);

Param[5].Value = TextBox6.Text;

=============================================

Thans.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s