Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Connecting to a SQL database from ASP .NET II

This is part II of the tutorial that teaches you how to connect to a SQL database from ASP .NET using SQL Authentication. This part shows you how to connect to the database using ASP .NET code and Visual Studio .NET's features.

On Saturday, August 21st 2004 at 07:16 AM
By Andrew Pociu (View Profile)
****-   (Rated 3.7 with 82 votes)
Contextual Ads
More ASP.NET Resources
Advertisement
This is part II of the tutorial named: Connecting to SQL from ASP .NET I




In the first part of the tutorial we dealed with the SQL database. Now we're going to make the ASP .NET web application that will establish a connection to the database we created. We're going to do it the Visual Studio way - as quick as possible with very few effort.



So fire up Visual Studio .NET and create a new ASP .NET Web Application - name it 'SQLConnection'.



Do a Ctrl+Alt+S shortcut that brings the Server Explorer window. Browse through Server > YourComputerName > SQL Servers > YourInstanceName.





NOTE

If you cannot see the instance name (in the picture on the left the instance name is WORKSTATION - the second, written all in uppercase), right click SQL Servers and choose Register SQL Server Instance. Leave the default text in the fields and a new instance will be created using your computer's name.



We are interested in the MyDB database, and if you click the [+] sign next to it you can see that it is freely browseable. It can be browsed because Visual Studio authenticates using Windows NT Integrated Security but one of the purposes of this tutorial is to connect to it using SQL Authentication (in fact, that's why we created that SQL login in the first tutorial).

We can simply connect to MyDB using different authentication: right click the database name and choose Change Login... uncheck the checkbox named 'Use Windows NT Integrated Security' and in the Login field enter the login name that we created in the first tutorial (aspnet) and the password you chose:







Everything should work just fine and you should be able to browse the database now, just as you did using Windows Integrated Authentication.

You can freely browse the database in the Server Explorer:








Server Explorer also shows us the table and the columns. What's great about this is that you can actually drag and drop the columns and even the table on the design area of the Web Application. And this is what we're going to do, as I said earlier, we're going to do this the Visual Studio way - quick and simple.

So grab the MyLinks (dbo) item and drop it on the design area. You are warned that the password is saved as clear text - ignore and choose Include password. Now a Data Adapter Configuration Error pops up that tells us it couldn't generate the UPDATE and DELETE statements. We don't mind, we only want to retrieve data from the database right now.



sqlConnection1 and sqlDataAdapter1 are created. Now we only have to code the part where we retrieve rows from the database. And thanks to Visual Studio .NET, it won't take long.



Open the code behind file (WebForm1.aspx.cs) and inside Page_Load use the following code:





// Open the connection

sqlConnection1.Open();

// Create a new DataReader and assign it to select command

System.Data.SqlClient.SqlDataReader sqlDataReader1 = sqlSelectCommand1.ExecuteReader();

// Loop through the DataReader

while(sqlDataReader1.Read())

{

   // Display the value inside the Title column

   Response.Write(sqlDataReader1["Title"] + " - ");

   // And the value inside the URL column

   Response.Write(sqlDataReader1["URL"] + "<br />");

}



Compile and see the result in Internet Explorer:







We successfully connected to a SQL database from an ASP .NET application using SQL Authentication!

Get yourself a beer or whatever you may drink and enjoy your accomplishment.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by ctlim on Monday, March 21st 2005 at 09:42 PM

Demand: Can you convert the following code into vb. Thanks.
// Open the connection

sqlConnection1.Open();

// Create a new DataReader and assign it to select command

System.Data.SqlClient.SqlDataReader sqlDataReader1 = sqlSelectCommand1.ExecuteReader();

// Loop through the DataReader

while(sqlDataReader1.Read())

{

// Display the value inside the Title column

Response.Write(sqlDataReader1["Title"] + " - ");

// And the value inside the URL column

Response.Write(sqlDataReader1["URL"] + "");

}

by sdf on Monday, March 28th 2005 at 05:16 AM

asdfadsf

by Rodney on Tuesday, March 29th 2005 at 07:47 AM

Great tutorial! Thanks

by Javier on Monday, August 15th 2005 at 09:18 PM

┬┐como guardo datos en la base?

how save datas on the DB?

by Rajendran on Wednesday, October 5th 2005 at 06:02 AM

Simple and best, I like this site...

by abiola on Thursday, October 13th 2005 at 06:54 AM

Good job! You guys have exposed me to how to connect to msde database. God bless u all.

by -_- on Wednesday, November 9th 2005 at 04:49 AM

I follow all the steps, everything goes very fine but except the last step to view the page in IE, error msg: SQL Server does not exist or access denied.

Pls advise, many thanks!!!

:

by :) on Wednesday, November 16th 2005 at 04:39 AM

I believe that the cause of this error is that SQL Server does not exist or access denied.

by bsrikanth on Sunday, December 11th 2005 at 02:15 AM

hwo to write a function to connect any database, any table

by kin on Tuesday, December 20th 2005 at 08:34 AM

It is a very very good tutorial. I follow all the steps but
the code behind file is a bit confusing. The extension for
my code behind file is WebForm1.aspx.vb and not WebForm1.aspx.cs. I tried to copy the code and compile
all the same but generated a lot of errors. Please help.
thanks in advance!

by Andrei Pociu on Tuesday, December 20th 2005 at 08:41 AM

You are using Visual Basic .NET, and this is a C# tutorial. You'll need to create an C# ASP.NET website instead of a Visual Basic .NET ASP.NET website for the code to work. You can choose the type of project in Visual Studio when you create a new solution.

Then the code-behind file will have the .aspx.cs extension and the code will work.

Good luck!

by kin on Tuesday, December 20th 2005 at 09:50 AM

Great job Andrei ! It works using c#. Do you know any useful tutorial that address connecting to MsSQL using
vb.net?

by Andrei Pociu on Tuesday, December 20th 2005 at 10:05 AM

The sample code at http://www.startvbdotnet.com/ado/sqlserver.aspx should do it.

by manoj dhamija on Thursday, February 9th 2006 at 11:23 PM

hey great tutorial it helps me alot

by srinivasan on Thursday, February 23rd 2006 at 06:29 AM

Db connection works fine. thanks alot frienz

by chandra on Thursday, April 20th 2006 at 09:25 AM

excellent

by r6r564 on Monday, December 18th 2006 at 09:50 AM

nice

by friend on Thursday, February 22nd 2007 at 05:10 AM

hii
This is the worst article that i have ever read
dont ever publish this again
beware bye


frnd

by haha on Thursday, February 22nd 2007 at 05:10 AM

hii
hahaha

by worstttttttttttttttt on Thursday, February 22nd 2007 at 05:11 AM

This is the worst article that i have ever read
dont ever publish this again
beware bye

by Ajay Vaid on Friday, April 27th 2007 at 05:34 AM

I Like It ,Its Really HelpFul For Me
thanks

by PREM on Thursday, May 17th 2007 at 10:04 AM

Thank you...excellent

by VB.Net on Thursday, June 14th 2007 at 02:19 PM

' Open the connection

sqlConnection1.Open()

' Create a new DataReader and assign it to select command

Dim sqlDataReader1 As System.Data.SqlClient.SqlDataReader = sqlSelectCommand1.ExecuteReader()

' Loop through the DataReader

While sqlDataReader1.Read()


' Display the value inside the Title column

Response.Write(sqlDataReader1("Title") + " - ")

' And the value inside the URL column


Response.Write(sqlDataReader1("URL") + "")
End While

by Glory Ukoh on Monday, July 2nd 2007 at 06:29 AM

Thank goodness, you guys just let my cat out of its bag; I am overlly impressed. Thanks

by Razareal on Thursday, July 5th 2007 at 01:08 AM

It's very helpful for me to start my work unsing ASP.NET and SQL SERVER.

by arun on Thursday, August 16th 2007 at 03:29 AM

its very useful for my career

by saranya on Thursday, December 6th 2007 at 12:44 AM

Regarding ASP.Net i have one doubt,when i am trying to connect the webapplication with sql server database it showing the following error
Server Error in '/Sample' Application.

--------------------------------------------------------------------------------

INSERT permission denied on object 'Info', database 'master', schema 'dbo'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: INSERT permission denied on object 'Info', database 'master', schema 'dbo'.

but in windows application i can able to work
please give one solution

by Pez on Thursday, December 6th 2007 at 01:20 AM

Looks like you don't have permission over the Insert command. Check the user's credentials.

by Balaji.V.P on Sunday, January 6th 2008 at 04:52 AM

I need the query "How to connect the two and more table in the database and how can fetch the data from the connected database.
I am using SQL 2000 Server

by dcx on Friday, February 1st 2008 at 12:55 AM

dfg

by Rishh on Thursday, March 27th 2008 at 06:13 AM

I have 1 doubt in sql server.I made a website on asp.net and when I tried 2 connect to sql server it gave an error #26..how to resolve it?It says that sql server does not allow remote connections..

by suraj manbahadur thadarai on Monday, June 2nd 2008 at 08:09 AM

doesn't clear my doubts at all

by asdf on Monday, June 23rd 2008 at 02:03 AM

dffsdf

by keka on Monday, August 18th 2008 at 06:26 AM

keko keka

by surender on Tuesday, March 24th 2009 at 03:35 AM

not satisfied

by Deepak on Thursday, April 16th 2009 at 07:18 AM

Hello
I want the connectivity code withsql server in asp.net application with is property.

Plzzzzzzzz
help its really very important to me..........

by Shah Rukh Khan on Thursday, June 18th 2009 at 03:50 PM

After reading this code, I serioulsy wish that I was a programmer rather than an actor. Great minds!!!

by sejal on Wednesday, July 15th 2009 at 05:34 PM

Certinaly, it helped, if you want the code in vb, get the converter from google

by sejal on Wednesday, July 15th 2009 at 05:34 PM

Certinaly, it helped, if you want the code in vb, get the converter from google

by subhani on Monday, July 27th 2009 at 03:30 AM

hi this is subhani ,i fell ver happy.and i need qurees on sql server 2000. plz send me pdfs

by jega on Saturday, October 10th 2009 at 07:33 AM

hi friends,
i want to know how to connect sql 7.0 to Asp.net .i know only through c# ,but can you tell how through vb

by Akash on Tuesday, October 27th 2009 at 07:10 AM

Hi friends,

I just wanna sql database connection by asp.net using c#...

please send me some pdf tutorials with examples...

Thanx a lot....

by jayan on Friday, October 30th 2009 at 12:25 PM

Error 1 The name 'sqlConnection1' does not exist in the current context C:\Documents and Settings\jayan\My Documents\Visual Studio 2008\Projects\SQLConnection1\SQLConnection1\Default.aspx.cs 14 13 SQLConnection1

by jayan on Friday, October 30th 2009 at 09:15 PM

Error 1 A namespace does not directly contain members such as fields or methods C:\Documents and Settings\jayan\My Documents\Visual Studio 2008\Projects\SQLconnection\SQLconnection\Default.aspx.cs 3 1 SQLconnection

by Afomia on Monday, November 9th 2009 at 03:49 AM

I want upload and download files in sql data table using asp.net with C# and also how i can manage or delete,retrieve,edit and update the uploaded file please send me tutorial with code

by ...k\ on Monday, December 21st 2009 at 12:27 AM

./,./.,

by RajuGS on Friday, February 5th 2010 at 01:46 AM

Sir
i want db connections using asp.net globally
web.config file but i unable to access these connection to our remaining business logics forms

by abhi on Monday, February 22nd 2010 at 11:32 PM

thanks

by jaistealth on Tuesday, February 23rd 2010 at 08:21 PM

Public Function CreateChart() As String

Dim util As New Utilities.Util()
'Database Objects - Initialization
Dim oRs As DbConn, strQuery As String
'strXML will be used to store the entire XML document generated
Dim strXML As String

'Generate the graph element
strXML = ""
'strXML = ""

' SQL Query
strQuery = "select ITEM,Cost from WholeSaleCost"

' Open Data Reader
oRs = New DbConn(strQuery)
'Iterate through each factory
If oRs.ReadData Is Nothing Then
Response.Write(" Connection error")
Response.End()
Return ""
End If
While oRs.ReadData.Read()

'Generate
strXML = strXML

by Shahriar Nour Khondokar on Monday, March 29th 2010 at 11:09 AM

This is a very good article.

For a similar article on connecting to SQL Server from C#.NET and Java, embedding SQL, calling stored procedures, passing parameters, retrieving results etc, check: http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html

Shahriar Nour Khondokar: http://www.shahriarnk.com/

by Kartik on Friday, April 2nd 2010 at 03:35 PM

Can you write the code in Visual Studio 2008 and with SQL Server 2005 Compact Edition (.sdf database)?

by mightygg on Monday, April 26th 2010 at 08:37 AM

plixx help me with code for connecting asp.net to sql server. plizz help

by shaimaa on Wednesday, April 28th 2010 at 08:00 PM

how to connectdatabase a asp.net

by Ervin on Tuesday, May 18th 2010 at 02:07 AM

the first part of this tutorial is very useful.

by kgfkg on Friday, June 25th 2010 at 06:08 AM

gfcmb.c

by mightygg on Friday, June 25th 2010 at 07:45 AM

plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

PLIXX HELP ME GUYS

by mightygg on Friday, June 25th 2010 at 07:46 AM

plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

PLIXX HELP ME GUYS

by mightygg on Friday, June 25th 2010 at 07:46 AM

plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

PLIXX HELP ME GUYS

by mightygg on Friday, June 25th 2010 at 07:46 AM

plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

PLIXX HELP ME GUYS

by mightygg on Friday, June 25th 2010 at 08:05 AM

plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

PLIXX HELP ME GUYS

by mightygg on Friday, June 25th 2010 at 08:05 AM

plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

PLIXX HELP ME GUYS

by mightygg on Friday, June 25th 2010 at 08:05 AM

plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

PLIXX HELP ME GUYS

by aditya on Thursday, August 26th 2010 at 10:46 PM

hi m aditya

by PRIYA on Wednesday, October 6th 2010 at 05:37 AM

hi,
HOW TO CONNECT THE DATABASE IN ASP.NET

by anoop on Saturday, October 9th 2010 at 04:49 AM

thanks

by sheeba on Tuesday, January 11th 2011 at 12:04 AM

hi..

by kjhl on Wednesday, January 12th 2011 at 01:30 AM

twyjfkkikkkkkkkkkk

by kjhl on Wednesday, January 12th 2011 at 01:30 AM

twyjfkkikkkkkkkkkk

by banu on Wednesday, January 12th 2011 at 01:33 AM

fine but cant get anything

by revathy on Wednesday, February 2nd 2011 at 10:04 PM

nice and clear

by Rahul sharma on Thursday, February 10th 2011 at 05:45 AM

thanks alot. It help me to create a connection in asp.net

by i on Saturday, March 5th 2011 at 05:32 AM

5

by i on Saturday, March 5th 2011 at 05:32 AM

5

by suja on Sunday, March 6th 2011 at 04:20 AM

hi guys m making an website for Crime Branch Mumbai n m having lots of doubts regarding ASP.NET and C# plz if it is possible from ur side to help me in my project i'll b very happy......
the first thing is i want some ideas for my website related to anything like background, contents or entire layout plz...plz...plz...guys i dont have time more than 5 days its urgent n also one major problem that, i want to connect my each control i.e. text box, combo-box value n selected item from drop-down list to my each coloum,row in sql. plz send me any video or any tutorial so that i can make my project plz..plz...plz.....
bye thanks
suja.....

by suja on Sunday, March 6th 2011 at 04:21 AM

hi guys m making an website for Crime Branch Mumbai n m having lots of doubts regarding ASP.NET and C# plz if it is possible from ur side to help me in my project i'll b very happy......
the first thing is i want some ideas for my website related to anything like background, contents or entire layout plz...plz...plz...guys i dont have time more than 5 days its urgent n also one major problem that, i want to connect my each control i.e. text box, combo-box value n selected item from drop-down list to my each coloum,row in sql. plz send me any video or any tutorial so that i can make my project plz..plz...plz.....
bye thanks
suja.....

by karthik on Wednesday, April 6th 2011 at 01:03 AM

Hi Guys m trying to connect the database with asp.net using C# code can anybody help me pls....... still now i con't able to connect plzzzz.....

by suja on Saturday, April 9th 2011 at 04:26 AM

hey guys its not working......
ur code is throwing an error...........
n ya one more thing m working with visual studio 2010 forntend, sql 2005 backend n their r lots of new things which r confusing so...........
can u plz do something or give me such a code which will work on 2010 plz plz hurry i got some more time from my senior officers coz i had my exams in last month so now m waiting plz help me............
n also send me some of the websites link which will help me in the designing of my website coz it looks pathetic.
plz plz plz...............

suja

by personal loans on Thursday, September 22nd 2011 at 11:22 PM

According to my own investigation, thousands of persons all over the world receive the business loans from various banks. Thence, there is a good possibility to find a college loan in every country.

by mortgage loans on Tuesday, September 27th 2011 at 05:28 AM

It is good that we are able to get the mortgage loans and this opens up completely new possibilities.

by home loans on Thursday, October 6th 2011 at 01:29 AM

People all over the world receive the business loans from different creditors, because it is easy.

by abongile on Friday, October 14th 2011 at 04:27 AM

nice site

by abongile on Friday, October 14th 2011 at 04:27 AM

nice site

by abongile on Friday, October 14th 2011 at 04:27 AM

nice site

by qwew on Thursday, November 3rd 2011 at 07:16 AM

hhjhjhjhjhjh

by magazine subscriptions- on Monday, December 12th 2011 at 04:58 AM

Looks like you don't have permission over the Insert command. Check the user's credentials.and plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

by magazine subscriptions- on Monday, December 12th 2011 at 04:58 AM

Looks like you don't have permission over the Insert command. Check the user's credentials.and plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

by john on Saturday, January 14th 2012 at 01:12 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by john on Saturday, January 14th 2012 at 01:13 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by gdpoker.oneminutesite.it on Sunday, January 22nd 2012 at 07:27 PM

I loved as much as you will receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike.

by best websites on Tuesday, February 21st 2012 at 07:38 AM

Looks like you don't have permission over the Insert command. Check the user's credentials.and plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

by mnbm on Monday, April 2nd 2012 at 06:14 AM

nvbnvb

by mnbm on Monday, April 2nd 2012 at 06:14 AM

nvbnvb

by nody@unlocked cell phones on Wednesday, April 25th 2012 at 07:12 PM

mast Article.........)

by shweta on Sunday, May 6th 2012 at 09:00 AM


Error:connectionstring is not been initialized
plz can you tell me about this error.

by replica Louis Vuitton handbags on Monday, May 21st 2012 at 09:52 PM

Good post!I was very pleased to find this site.I wanted to thank you for this great read!

by on Monday, June 11th 2012 at 06:20 PM

asdads


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources