954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating sql database connection in asp.net web application through C#

Following I am mentioning step-by-step procedure to create database connection to the asp.net website textboxes via C#.

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 Employee.

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

anujtripathi
Newbie Poster
7 posts since Dec 2008
Reputation Points: 7
Solved Threads: 1
 

There are too many errors in the code...pz check..even the table name is employees not students ..?

NidaNovice
Newbie Poster
1 post since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

Hi Nida,
Thanks for updating this article, I agree with the ambiguity regarding table name , can you please let me know about any other error you find in this article. It'll help me to improve it.

Thanks again for your reply.

Best Regards,
Anuj

anujtripathi
Newbie Poster
7 posts since Dec 2008
Reputation Points: 7
Solved Threads: 1
 

This is not a Right Place to post Articles. There should be Articles Section. You are confusing the users

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

sir i create my database in ms access and i store it in my d drive then i connect it then it connect but when i store my website on my web space then where i load my database and what path i should use to connect my ms access data base through coding in asp.net

artipathak
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

sir i create my database in ms access and i store it in my d drive then i connect it then it connect but when i store my website on my web space then where i load my database and what path i should use to connect my ms access data base through coding in asp.net

artipathak
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Where did you put your Database File ?

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 
sir i create my database in ms access and i store it in my d drive then i connect it then it connect but when i store my website on my web space then where i load my database and what path i should use to connect my ms access data base through coding in asp.net


artipathak make a new thread and ask your question there.............
we'll happy to help you three......:)
it is already a solved thread..........

avirag
Posting Whiz
313 posts since Jun 2009
Reputation Points: 31
Solved Threads: 36
 

Now when you were still in your Development machine you took your mdb file and place it in your : drive , now when you move it to your paid domain , there will be no d: drive, Just create a folder in your app and name it "DB" and paste your DB inside there and when you access it in your connection string use

~/DB/myDb.mdb


and you can see how i did it here http://www.codeproject.com/KB/tips/StoringConstring.aspx

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 

how to fill the gridview columns with multiple queries result?
i mean first column will display the result from first query which returns
only 1 column....
2nd query result will b displayed in second column of same gridview n so on...

priyankakaur1
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 
how to fill the gridview columns with multiple queries result? i mean first column will display the result from first query which returns only 1 column.... 2nd query result will b displayed in second column of same gridview n so on...

Can you please create a Separate thread for this Question.

vuyiswamb
Posting Whiz
312 posts since Mar 2007
Reputation Points: 31
Solved Threads: 14
 
lal.ramesh
Newbie Poster
10 posts since Nov 2010
Reputation Points: 19
Solved Threads: 0
 

please tell me how to establish database(sql server 2005) connection in .net framework 4
(language c#)

aswanthkumar
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

sir, please tell me how can i add my database server name to the sql data source.
as your information by left clicking on the sqlDatasource i can't get any configureDataSource. and where to add the c # code is in aspx file or the aspx.cs file

Kalyani Bhosle
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

hello plz tell me how can coonnect database..........))

nodysmith
Newbie Poster
1 post since Apr 2012
Reputation Points: 0
Solved Threads: 0
 

i have one sql database, how can i view that database through internet

RajendranBe
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You