how to insert value into database

Raj_Chennai
Posted by Raj_Chennai under ASP.NET category on | Points: 40 | Views : 1118
default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
Sql_Connection con = new Sql_Connection();
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
string username = txtUserName.Text;
string pwd = TxtPwd.Text;
string rPwd = txtRpwd.Text;
string quest = ddlSecurityQuestions.Text;
string ans = TxtAnswer.Text;
con.open();
using (cmd = new SqlCommand("insert into Login values(@username,@password,@securityquestions,@answer)",con.cn))
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@username", SqlDbType.VarChar).Value = username;
cmd.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = pwd;
cmd.Parameters.AddWithValue("@securityquestions", SqlDbType.VarChar).Value = quest;
cmd.Parameters.AddWithValue("@answer", SqlDbType.VarChar).Value = ans;
cmd.ExecuteNonQuery();
lblMessage.Visible = true;
lblMessage.Text = "Registration completed sucessfully";
}
catch (SqlException s)
{
lblMessage.Visible = true;
lblMessage.Text = s.Message.ToString();
}
catch (Exception ee)
{
lblMessage.Visible = true;
lblMessage.Text = ee.Message.ToString();
}
finally
{
if (con.cn.State == ConnectionState.Open)
{
cmd.Dispose();
con.cn.Close();
con.cn.Dispose();

}
}
}
}

Sql_Connection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for Sql_Connection
/// </summary>
public class Sql_Connection
{
public Sql_Connection()
{
//
// TODO: Add constructor logic here
//
}
public SqlConnection cn;
public void open()
{
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["raj"].ConnectionString);
cn.Open();

}
public void close()
{
cn.Close();
}
}

Comments or Responses

Posted by: Self-Innovator on: 3/27/2012 Level:Bronze | Status: [Member] | Points: 10
POst it in Article Sec..

Login to post response