How to create connection Ado.net and SqlServer 2008
I want to create connection to insert data into database
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 7, 2013, 11:23 PM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Login_and_signup
{
public partial class Registration : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(connStr);
//sqlConnection.ConnectionString = @"Data Source=SS438\SQLEXPRESS;Initial Catalog=Examples;Integrated Security=true;";
sqlConnection.Open();
string sqlQuery = "INSERT INTO Users(FirstName,LastName,UserName,Password,Address) VALUES(@FirstName,@LastName,@UserName,@Password,@Address)";
SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
sqlCommand.CommandType = CommandType.Text;
sqlCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text.Trim();
sqlCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text.Trim();
sqlCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtUserName.Text.Trim();
sqlCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtPassword.Text.Trim();
sqlCommand.Parameters.Add("@Address", SqlDbType.VarChar).Value = txtAddress.Text.Trim();
int irows = sqlCommand.ExecuteNonQuery();
lblMessage.Text = "User inserted successfully...";
ClearFields();
}
protected void btnReset_Click(object sender, EventArgs e)
{
ClearFields();
}
private void ClearFields()
{
txtFirstName.Text = "";
txtLastName.Text = "";
txtUserName.Text = "";
txtPassword.Text = "";
txtAddress.Text = "";
}
}
}