Programming for Everybody: Retrieve data from sql server database in asp.net using VB.net

Retrieve data from sql server database in asp.net using VB.net





Imports System.Data.SqlClient

Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim constring As String = ConfigurationManager.ConnectionStrings("sportsconn").ConnectionString

        Dim conn As New SqlConnection(constring)

        conn.Open()

        Dim cmd As New SqlCommand("Select name1,age,sport,points,Format(date_reg, 'dd/MM/yyyy') As date_reg From table1 Where id=@id", conn)

        cmd.Parameters.AddWithValue("id", TextBox1.Text)

        Dim myreader As SqlDataReader

        myreader = cmd.ExecuteReader()

        If myreader.Read() Then

            Label7.Visible = False

            TextBox2.Text = myreader("name1").ToString()

            TextBox3.Text = myreader("age").ToString()

            TextBox4.Text = myreader("sport").ToString()

            TextBox5.Text = myreader("points").ToString()

            If Not (myreader.IsDBNull(myreader.GetOrdinal("date_reg"))) Then

                TextBox6.Text = myreader("date_reg")

            Else

                TextBox6.Text = ""

            End If

        Else

            Label7.Visible = True

            TextBox2.Text = ""

            TextBox3.Text = ""

            TextBox4.Text = ""

            TextBox5.Text = ""

            TextBox6.Text = ""

        End If

conn.close()

    End Sub

End Class 

Code in web config:

 <connectionStrings>

    <add name="sportsconn" connectionString="Data Source=.;Initial Catalog=sports;Integrated Security=true" providerName="System.Data.Sqlclient" />

  </connectionStrings>

No comments:

Post a Comment