What is CrazyEngico.com (CE)?

CrazyEngico (CE) is a one-stop solution for interview questions and answers, domain knowledge, e-learning, tutorials, articles and many more learning material.
e-learning - Intelligent e-learning tutorials and articles
Domain Knowledge Learn about various business domains/verticals such as Banking, Financial, Insurance, Healthcare and many more...
Interview Questions and Answers - Post your questions and answers. 1000+ database of ready questions and answers available to prepare for an interview.
Tutorials - 300+ video tutorials from beginner to expert level.
Articles - 300+ technical articles to understand concepts.
Forum - A technical forum to post your queries/responses.

Ads
Creating Sql Database Connection in Asp.net Web Application Through C#
Views: 2277 | Votes: 0        0

Creating Sql Database Connection in Asp.net Web Application Through C#

Author: Anuj Tripathi

1: Open visual studio 2005 and from FILE select NEW-> WebSite, then from pop-menu select asp.net website (Note: Language must be C#) , then press OK.

2: Now create three textboxes from the toolbox , to remove ambiguity I am using default name for that textboxes and that should be textbox1,textbox2 & textbox3. And also drag & drop the button to the asp.net application.

3: Meanwhile, Open SQL SERVER Management studio 2005 and open adventureworks(or you can create your own database by the Sql command CREATE DATABASE database_name), but here I am using AdventureWorks database.

4: Then create table by clicking right mouse button and form three column, Name them according to your requirement , for your information only I used EID,name,Address. And click save button, then you'll get a pop up menu where you have to enter the Table Name, for this example I used  students.

5: Now Come Back to Visual studio Application and write the following code in the button_click event

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = "Connection string(See Below for this —
)COPY PASTE  HERE";

myConn.Open();
string strqry = "Insert into students values (" + TextBox1.Text +
",'" + TextBox2.Text + "‘,'" + TextBox3.Text + "‘)";

SqlCommand myCom = new SqlCommand(strqry, myConn);
int numrow = myCom.ExecuteNonQuery();
myConn.Close();

}


Kindly note down the connectionString Syntax, to obtain this string do the following steps :
Step 1 : Go to toolbox and from data section drag & drop SqlDataSource.
Step 2: Then on left clicking the mouse on SqlDataSource , you will get configureDataSource link, Slect this.
Step 3: NewConnection String -> Provide Server Name then select or write database name , i.e. AdventureWorks.
Step 4: TestConnection and then Press OK.
Step 5 : Now you will again redirect to the page that you actually see in the step 2, but this time click + sign before the connection string and from the drop drown hierarchy you will receive the connection string.
Step 6: Just copy and paste this string to the place of connectionstring.
Now debug & run Your application and check it's effect on the table.
If still you face any problem feel free to ask.
Hope this explanation help you to understand the basics of database connectivity.
If you like this please comment it !
Thanks,
Anuj

Article Source: http://www.articlesbase.com/programming-articles/creating-sql-database-connection-in-aspnet-web-application-through-c-692926.html

About the Author

Anuj Tripathi
Success comes to those who dares

I am a Computer Engineer, right now I am working as a software Engineer


Copyright © 2017 CrazyEngico.com. All rights reserved.
CrazyEngico.com is not responsible for the content and CrazyEngico.com does not evaluate or guarantee the accuracy of any CrazyEngico.com content.
Please read our terms of service agreement before using this site.

feedback