title: can't read data from database in asp.net using vb
body: <p>i am learning asp.net using vb,i am working on college assignment 
  i have to read data from msaccess and show record in label
  basically i am doing 'search' operation</p>
  
  <p>here is the code</p>
  
  <p>Public Class WebForm2
      Inherits System.Web.UI.Page
      Dim connection As New OleDb.OleDbConnection
      Dim sname, lname, city, subj, gender, hobbies, religion As String
      Dim age, phone As Integer</p>
  
  <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
  
      connection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\himzz\Documents\Database1.accdb;")
      connection.Open()
  
      If Not IsPostBack Then
  
  
      End If
  End Sub
  
  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
  
      sname = TextBox1.Text
      lname = TextBox2.Text
      age = TextBox3.Text
      city = TextBox4.Text
      phone = TextBox5.Text
      subj = DropDownList1.SelectedValue
  
      If RadioButton1.Checked Then
          gender = "male"
      ElseIf RadioButton2.Checked Then
          gender = "female"
      Else
          gender = "other"
      End If
  
      'If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked Then
      '    hobbies = "cricket swimming tennis "
      'End If
  
      If CheckBox1.Checked Then
          hobbies += "cricket "
      End If
  
      If CheckBox2.Checked Then
          hobbies += "swimming "
      End If
  
      If CheckBox3.Checked Then
          hobbies += " tennis "
      End If
  
      religion = DropDownList2.SelectedValue
  
  
  
      Try
          Dim q As String = "insert into stud (sname,lname,age,city,phone,subj,gender,hobbies,religion) values('" &amp; sname &amp; "','" &amp; lname &amp; "','" &amp; age &amp; "','" &amp; city &amp; "','" &amp; phone &amp; "','" &amp; subj &amp; "','" &amp; gender &amp; "','" &amp; hobbies &amp; "','" &amp; religion &amp; "' )"
          Dim cmd As New OleDb.OleDbCommand(q, connection)
          cmd.ExecuteNonQuery()
          MsgBox("submitted")
  
      Catch ex As Exception
          MsgBox("error")
      End Try
      Response.Redirect(Request.Url.AbsoluteUri)
  
  End Sub
  
  Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
  
  
  
      Dim q As String = "select lname from stud where sname='" &amp; sname &amp; "' "
      Dim cmd As New OleDb.OleDbCommand(q, connection)
      Dim reader As OleDb.OleDbDataReader
      reader = cmd.ExecuteReader()
  
  
  
  
      While reader.Read()
  (tried this but not worked)  ' Label12.Text = " " &amp; reader("sname")
  </code></pre>
  
  <p>(tried this also but not worked)' Label12.Text =reader.Item("sname").ToString()
  (not working)      Label12.Text = reader("sname").ToString</p>
  
  <pre><code>        'Label13.Text = "age " &amp; reader("age")
          'Label14.Text = "city " &amp; reader("city")
          'Label15.Text = "phone no " &amp; reader("phone")
          'Label16.Text = "subject " &amp; reader("subj")
          'Label17.Text = "Gedner " &amp; reader("gender")
          'Label18.Text = "Hobbies " &amp; reader("hobbies")
          'Label19.Text = "Religion " &amp; reader("religion")
  
  
      End While
  
      reader.Close()
  End Sub
  </code></pre>
  
  <p>End Class</p>