Thursday, March 22, 2012

Data retrieving or querying problem

Hi, I am using visual web developer 2005 express edition and Microsoft SQL 2005 to develop a fast-food ordering website and I am having trouble in retrieving data from a database table into a form. Can some one please teach me the way to write the syntax forretrieving or selecting a inserted value from the database? I have only know the syntax to insert data value from a form which is something like this:

'Create a New Connection to our daabase

Dim testAs SqlDataSource =New SqlDataSource()

test.ConnectionString = ConfigurationManager.ConnectionStrings(

"Connectionstring1").ToString'This is the SQL Insert Command

test.InsertCommand =

"Insert into Customer([Initial],[Correspondent_Name],[Correspondent_No],[Payment_Type]) VALUES (@.Initial,@.Correspondent_Name,@.Correspondent_No,@.Payment_Type)"'Each of this insert a value into the appropriate command

test.InsertParameters.Add(

"Initial", DropDownList4.SelectedValue)'This is the selected "Initial" DropDownList value

test.InsertParameters.Add(

"Correspondent_Name", TextBox10.Text)'This is the Correspondent_Name value

test.InsertParameters.Add(

"Correspondent_No", TextBox3.Text)'This is correspondent_no value

test.InsertParameters.Add(

"Payment_Type","Pay upon delivery")'This indicates that the payment is made with credit card

test.Insert()

But I have no idea the syntax to retrieve a data. Please help me with this matter as it is important to me. Thanks in advance!

Regards,

Ivan

Hi,

There are many ways to retrieve data from a database table into a form. It depends on what kind of controls would you like to bound with.

If you want to display the data in a table, the easist way is to use a GridView control and assign its DataSourceID with the SqlDataSource control's ID.

Another way is to use DataView to retrieve the set of data.eg:

DataSourceSelectArguments ar =new DataSourceSelectArguments();DataView dv = (DataView)SqlDataSource1.Select(ar);this.TextBox1.Text = dv.Table.Rows[0]["CategoryName"].ToString();

In this way you can retrieve the value of any row any column manually.

Thanks.

|||

One of the ways how can you select data from database is this:

Imports System.Data.SqlClient

Dim test As SqlConnection = New SqlConnection()
test.ConnectionString = ConfigurationManager.ConnectionStrings("Connectionstring1").ConnectionString
Dim command As SqlCommand = test.CreateCommand()
command.CommandText = "SELECT * FROM Customer"
Dim GV1 As GridView = New GridView()
test.Open()
GV1.DataSource = command.ExecuteReader()
GV1.DataBind()
test.Close()
form1.Controls.Add(GV1)

No comments:

Post a Comment