Retrieve Code C# in Visual Studio 2010

Retrieve Code C#-technirmiti

In our previous tutorial, we have seen how to insert data from ASP.NET web form to a database table. Now we will see how to retrieve a data from database table and display it in textbox’s. We have used Visual Studio 2010 and inbuilt SQL server database which comes with it. We have student details form and student details database table. So let’s go through the procedure step by step and learn Retrieve Code C#.

Step 1: Create a form

We will follow the same procedure as described in our previous post to create a web form. Design the web form for student details as shown below with the help of Labels and TextBox‘s. Keep a Button named ‘Search‘ which will be used to retrieve the details of students based on the Roll Number.

Retrieve Code C#-technirmiti

Step 2: Design a Database to store and retrieve the Text.

We will use the same database which we have created for our previous tutorial. The table structure is as shown in the following diagram:

Retrieve Code C#-technirmiti

Step 3: Fill the table with few student details.

Go to Server Explorer and look for Database.mdf (which we have created earlier) -> Tables -> Student(Table Name)

Right click on Student and select Show Table Data. Fill the table as shown below. We have added two records of two students.

Retrieve Code C#-technirmiti

Step 4: To write a Retrieve Code C# which will retrieve the student details from student table

We want to retrieve the Name and Address of a student when user enters Roll Number of that student. The SQL query for this task is:

Select Fname,Address from Student where Roll_Number = ‘1’

So, go to the Design View of web page and double click on the Search button. You will see the code page where you have to write a C# code. Paste the following code there under Button_Click event:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“DatabaseConnectionString1”].ToString());
con.Open();
SqlCommandcmd = new SqlCommand(“Select Fname,Address from Student where Roll_Number = ‘” + TextBox1.Text + “‘”, con);
SqlDataReaderdr;
dr = cmd.ExecuteReader();
dr.Read();
TextBox2.Text = dr[“Fname”].ToString();
TextBox3.Text = dr[“Address”].ToString();
con.Close();

Add these name spaces in the beginning of the code:

using System.Data.SqlClient;
using System.Configuration;

Step 5: Debug the web page and test it

  • Save the project and go to Debug -> Start Debugging.
  • Your web form will be displayed in a web browser.
  • Put Roll Number as ‘1’ in Roll Number TextBox and click on SearchButton.
  • Name and Address of the student with Roll Number ‘1’ will be displayed in respective TextBox’s.

Retrieve Code C#-technirmiti

We can also display the full table on a web page by using GridView Control. Follow the steps below:

  • Drag and drop a GridView control from Toolbox on the same web form.

Retrieve Code C#-technirmiti

  • Add this Retrieve Code C# under Button_Click event.

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“DatabaseConnectionString1”].ToString());
con.Open();
SqlDataAdapter Adp = new SqlDataAdapter(“Select * from Student where Roll_Number = ‘” + TextBox1.Text + “‘”, con);
DataTable Dt = new DataTable();
Adp.Fill(Dt);
GridView1.DataSource = Dt;    //GridView1 is the id of that GridView control.
GridView1.DataBind();
con.Close();
}

  • Run the form and test. Put Roll Number as ‘1’ and corresponding details of Roll Number 1 will be displayed in a GridView as shown in the diagram:

Retrieve Code C#-technirmiti

That’s all about the retrieve process in in ASP.NET website by using Visual Studio 2010 and C# code.

mm

About Sunayana Gawde

Sunayana Gawde is Project Manager at TechNirmiti Solutions and the Author at TechOpinion. She is a researcher in the field of Computer Science and Technology. She loves to write about Technology. You can follow her on Facebook Twitter and Google+.

Leave a Reply

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