Saturday, 10 March 2012

Select query operation

Till now we have seen how to perform insert,delete and update operation now next and last operation is select query.

Till now we have seen only one page project here i will tell you how to redirect from one page to another page in asp.net using two page project.In this section i will create two page project on first page user will submit his detail and on next page he will display all his detail.

1)Create form as shown below for submitting data to database.
 In above screen shot submit button is used for inserting data to database which we already have seen.If you forgot how to insert data in database please go to this link Insert data into database.
Redirect to display page button will redirect you to next page ie. page where you will display data using select query.


2)Now our first page is ready which is shown above but how to insert one more webform in your project?just refer following screenshot:




After clicking Add new item following window opens,select webform from given list and give name to this form as submit.aspx .
Now click on Add button now you will see in your solution explorer one more page submit.aspx as shown below:


3)Code use for redirecting from one page to another page is given below:
Response.Redirect("*.aspx")
 in our case it will be  Response.Redirect("submit.aspx").

So now double click Redirect to display page button and type this code in click button event:
Response.Redirect("submit.aspx")
4)Now open submit.aspx page from solution explorer and create form as shown below:

Here we will take help of roll no to retrieve remaining data from database:

4)Now double click Show data button present on submit .aspx page. and type following code in submit.aspx.vb page.

 See this is your new page so first include following imort statement on top of sumbit.aspx.vb page:
Imports System.Data
Imports System.Data.SqlClient


Now type following code in button click event of Show data:
Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("a").ToString())
        Dim sqlqry As String = "select Name,Gender,City from Table1 where Rollno=' " & TextBox1.Text & " '"
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader
        Try
            connection.Open()
            cmd = New SqlCommand(sqlqry, connection)
            dr = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                TextBox2.Text = dr("Name").ToString()
                TextBox3.Text = dr("Gender").ToString()
                TextBox4.Text = dr("City").ToString()
            End If
        Catch ex As Exception
            'Label2.Text = ex.Message
        Finally
            connection.Close()
        End Try


5)Now save and run your website:

Database after submitting above data is shown below:

Now click on Redirect to display page button on webbrowser enter roll no of which details you want to retrieve as shown below:
 Click on Show data button:
This completes your select operation.

No comments:

Post a Comment