How to retrieve data from database using C#

In there are many techniques through which you can retrieve data from database either your data is in sql server or MS Access. In this article we will discuss, How to retrieve data from database using C# and how can we get specific data from database and what are the basic techniques. Specific data means, when you will develop a big project in which lots of data in the table and you want to get specific data form your database then you can use this technique to find your data by searching their id.

We have discussed about grid view technique in the last article, this is also similar to that one. First of all you have to create tables in your database and stored some data by using forms.

Adding Gridview :

Step 1: 

After storing data in the database, open your designing page,  go to DATA option and select Gridview.You will see a table, on the top of the table there will be a small arrow, click there and go to GridView Taks and select New data source form the Choose Data Source .  here is a screen shot below:

Step 2:

If your will select New Data source there will be new tap opened with the named Data Source Configuration Wizard. In this tap you have to choose your Data source type. If your database in Access then you will go with Access Database. Here we will select SQL database because our database is in SQL server, so select SQL database and click on ok. When you will click on SQL database there will automatically show an Id (Sqldatasoure1) in the below text box. here is screenshot below:

Step 3:

In the next step you will choose your Data Connection. In this step, there will show a small arrow, click there and select your data connection and then click on next. In this project, we have select KIU_SQL because we have named earlier KIU_SQL when we were creating a database. So just select the connection that you have been given during creating a database.

Step 4:  

This step is very important because in this step you will select the table and as well as columns that will be shown on grid view. In this project, we have selected asp_record because this is our table name which we are going to display, and we have also click on *  because we need whole data from this table therefore we have selected here *. You can select specific columns that you want to display and click on next. a screen shot below:

After clicking on next you will test the query also on the next step and then click on finish, after that the table will be displayed on the designing a page.

Step 5:

In this step, we will add parameters and insert some queries to get specific data from the database. So right-click on SqlDataSoure and go to properties, from the properties go to SelectQuery and then click there,  as shown in the below screenshot.

Step 6:

After clicking to Select Query you will see a new tap with the name, Command, and Parameter editor. In this step we will add parameters and as well as select commands. So Add parameter and go to parameter source, select Control from there, then go to ControlID, and select the text box name, here we have selected TextBox because this is our text box name. So select control id and then click on OK. here is a screenshot below:

Step 7:

 In the last step, you will see how the data will retrieve from the database by searching a unique id. Now go to you asp design page and run your page. When your page will open on the browser enter id on the search text box and press search you will see your specific data. Have a look at the below screen shot:

Designing Phase:

These are the steps through which you can get your specific data from the database. Hopefully, this article will be helpful for you and you will do this with no trouble.

Leave a Reply

Your email address will not be published. Required fields are marked *