A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
Connecting to a SQL database from ASP .NET IIThis 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.6 with 61 votes) |
||
|
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.
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:
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 It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsDemand: 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"] + "<br />");
}
asdfadsf
Great tutorial! Thanks
¿como guardo datos en la base?
how save datas on the DB?
Simple and best, I like this site...
Good job! You guys have exposed me to how to connect to msde database. God bless u all.
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!!!
:<
I believe that the cause of this error is that SQL Server does not exist or access denied.
hwo to write a function to connect any database, any table
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!
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!
Great job Andrei ! It works using c#. Do you know any useful tutorial that address connecting to MsSQL using
vb.net?
The sample code at <a href="http://www.startvbdotnet.com/ado/sqlserver.aspx" target="_blank">http://www.startvbdotnet.com/ado/sqlserver.aspx</a> should do it.
hey great tutorial it helps me alot
Db connection works fine. thanks alot frienz
excellent
nice
hii
This is the worst article that i have ever read
dont ever publish this again
beware bye
frnd
hii
hahaha
This is the worst article that i have ever read
dont ever publish this again
beware bye
I Like It ,Its Really HelpFul For Me
thanks
Thank you...excellent
' 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") + "<br />")
End While
Thank goodness, you guys just let my cat out of its bag; I am overlly impressed. Thanks
It's very helpful for me to start my work unsing ASP.NET and SQL SERVER.
its very useful for my career
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
Looks like you don't have permission over the Insert command. Check the user's credentials.
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
dfg
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..
doesn't clear my doubts at all
dffsdf
keko keka
not satisfied
Hello
I want the connectivity code withsql server in asp.net application with is property.
Plzzzzzzzz
help its really very important to me..........
After reading this code, I serioulsy wish that I was a programmer rather than an actor. Great minds!!!
Certinaly, it helped, if you want the code in vb, get the converter from google
Certinaly, it helped, if you want the code in vb, get the converter from google
hi this is subhani ,i fell ver happy.and i need qurees on sql server 2000. plz send me pdfs
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
Hi friends,
I just wanna sql database connection by asp.net using c#...
please send me some pdf tutorials with examples...
Thanx a lot....
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
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
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
./,./.,
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
thanks
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 = "<graph caption='WHOLESALE-REPORT' xAxisName='ITEM' yAxisName='COST'numberprefix='$' palette='3' showValues='0' formatNumberScale='0' showBorder='1'>"
'strXML = "<graph caption='wholesalepricereport' subCaption='item' decimalPrecision='0' showNames='1' numberSuffix='Units' pieSliceDepth='30' formatNumberScale='0'>"
' 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("<br> Connection error")
Response.End()
Return ""
End If
While oRs.ReadData.Read()
'Generate <set name='..' value='..' />
strXML = strXML
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/
Can you write the code in Visual Studio 2008 and with SQL Server 2005 Compact Edition (.sdf database)?
plixx help me with code for connecting asp.net to sql server. plizz help
how to connectdatabase a asp.net
the first part of this tutorial is very useful.
gfcmb.c
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
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
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
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
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
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
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
hi m aditya
Related Tutorials
Related Source Code
ASP.NET Job Search