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

Recommended Answers

All 27 Replies

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

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

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

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

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

Where did you put your Database File ?

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

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

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

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.

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

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

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

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

Give the namespaces shoud be imported for database connection

Thanks anujtripathi.

build a setup it install anoter system it's not connect database

i create a setup on windows application in c#.net projects(visual Studio 2010) this setup are another System(PC) in instal complitely work but it database are not display (error :- Connection string error)

very very thanks....i got brief idea about connection..

as per your code having error like this plz make sure reply fast

Sir i wanna ask 1 question in the parameters methode as like
cmd.parameter.addwithvalue("@rno",rno);
here why i am using @ ? what its importance ?
what if i dont use it?

Here is a code how to create SQL database connection in asp.net web application through C#. Check out......

using (SqlConnection con = new SqlConnection("SomeConnectionString"))
    {
      var cmd = new SqlCommand("select from myTable where myPK==N'"+ simpleText+ "'",con);
      cmd.Connection.Open();
      var sqlReader = cmd.ExecuteReader();
      while(sqlReader.Read())
      {
        //Fill some data like : string result = sqlReader("SomeFieldName");
      }
      sqlReader.Close();
      cmd.Connection.Close();
      cmd.Dispose();
    }

I hope it is helpful..........thanks!!
Read here:
http://www.asp.net/get-started
http://www.keenesystems.com/Expertise/ASPNetWebApplications.aspx
https://msdn.microsoft.com/en-us/library/ms178371%28v=vs.100%29.aspx

I have a problem. When i m working with d databases which are alrdy provided by the sql server 2005 dn its working properly bt when i m using the databases which are created by me dn asp.net failed to connect with the server.plz give some suggestion

@gouri kundu, Please post your connection code here. I will check and then able to give you an advice.......

Thnk u....bt i solved d problem.....actualy d problem ws I use a table with name user... then i jst put the table name with in []....& its working properly nw

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.