Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to insert Data in SQL server via SqlDataSource in ASP.NET

, 18 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to insert Data in SQL server via SqlDataSource in ASP.NET
  1. First of all, take a new website project.
  2. Insert two lables, two Textboxes and a Button.
  3. Rename thier ID like = lblname,lblFName, txtName, txtFName and btnInsert.
  4. Create database in sqlServer 2005 named EMPDB
  5. Create the Table emp having 3 fields:
    • ID = variable type int is identity = YES
    • Name = varchar(50)
    • F_Name = varachar(50)
  6. Insert from toolbox->data->sqlDataSource.
  7. Rename sqlDataSource to SqlDS.
  8. Now double click on button and write the following code:
    protected void btnInsert_Click(object sender, EventArgs e)
            SqlDS.InsertCommandType = SqlDataSourceCommandType.Text;
            SqlDS.InsertCommand = "Insert into emp (Name,F_Name) VALUES (@Name, @F_Name)";
            SqlDS.InsertParameters.Add("Name", txtName.Text);
            SqlDS.InsertParameters.Add("F_Name", txtFName.Text);
            txtName.Text = "";
            txtFName.Text = "";

  9. Now the record will be inserted to the database.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sajid Asghar

United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pinmembersigja6-Aug-14 10:58 
Questiongood Pinmemberkali2rk20-May-13 17:42 
QuestionHello World! PinmemberTomkoChoinski12-Jan-13 9:33 
Hello World!
AnswerRe: Hello World! PinmemberBjarne Nielsen28-Apr-13 10:01 
GeneralRe: Hello World! PinmemberTomkoChoinski2-May-13 12:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150128.1 | Last Updated 18 Feb 2012
Article Copyright 2012 by Sajid Asghar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid