INSERTING RECORDS IN SQL SERVER IN ASP.NET


INSERTING RECORDS IN SQL SERVER

                                                      User Interface  <. aspx>
                                                   -------------------
Code File:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient ;
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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Trusted_Connection=true;DataBase=Praveen_DataBase");
        SqlCommand cmd = new SqlCommand("Insert into Table1 values(@Name,@Address,@Course,@Roll)",con );
        SqlParameter obj1 = new SqlParameter("@Name", DbType.StringFixedLength);
        obj1.Value = TextBox1.Text;
        cmd.Parameters.Add (obj1);
        SqlParameter obj2 = new SqlParameter("@Address", DbType.StringFixedLength);
        obj2.Value = TextBox2.Text;
        cmd.Parameters.Add (obj2);
        SqlParameter obj3 = new SqlParameter("@Course", DbType.StringFixedLength);
        obj3.Value = TextBox3.Text;
        cmd.Parameters.Add (obj3);
        SqlParameter obj4 = new SqlParameter("@Roll", DbType.Int32.ToString());
        obj4.Value = TextBox4.Text;
        cmd.Parameters.Add (obj4);
        con.Open();
        cmd.ExecuteNonQuery();
        Label1.Text = "Record is Inserted";
        con.Close();

    }
}

0 comments:

Post a Comment