4 Replies - 8730 Views - Last Post: 26 September 2011 - 02:59 AM Rate Topic: -----

#1 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Value from Database to textbox

Posted 25 September 2011 - 11:59 PM

Hello..

I am new to vb.net. Need to know how can I retrieve the value from compname field of my table, to the textbox7 in form i made.
While the user enters data in the textbox, i need to compare the entered textbox value, if it exists or not, in the database. If exists, It should enter the value in textbox. If not, it should pop up a message box asking user to add new contact.

I have inserted data using Oledbd command string.

Database : MS Access 2007

Kindly help me out. Your guidance would be highly appreciated.

Thank You..

Is This A Good Question/Topic? 0
  • +

Replies To: Value from Database to textbox

#2 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: Value from Database to textbox

Posted 26 September 2011 - 01:28 AM

Hi,
Private Sub textbox7_leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.leave

        Try
              cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
            cn.Open()
            cmd = New OleDbCommand("SELECT compname  FROM table_name WHERE compname = '"& textbox7.text &"'", cn)
            OleDbDataReader oReader=cmd.ExecuteReader();            
        IF(oReader.HasRow())THEN

       While oReader.Read
          textbox7.text= oReader["compname"].ToString()
       End While

      ELSE

            MsgBox("add new contact.")

      Catch myException As Exception

          MsgBox("No Record Inserted" + myException.ToString())

      Finally
           cn.Close()
     End Try

    End Sub



Was This Post Helpful? 1
  • +
  • -

#3 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Value from Database to textbox

Posted 26 September 2011 - 01:48 AM

View PostVishu Sukhdev, on 26 September 2011 - 01:28 AM, said:

Hi,
Private Sub textbox7_leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.leave

        Try
              cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
            cn.Open()
            cmd = New OleDbCommand("SELECT compname  FROM table_name WHERE compname = '"& textbox7.text &"'", cn)
            OleDbDataReader oReader=cmd.ExecuteReader();            
        IF(oReader.HasRow())THEN

       While oReader.Read
          textbox7.text= oReader["compname"].ToString()
       End While

      ELSE

            MsgBox("add new contact.")

      Catch myException As Exception

          MsgBox("No Record Inserted" + myException.ToString())

      Finally
           cn.Close()
     End Try

    End Sub




Hello.. Thanks for reply..

but this line :

textbox7.text= oReader["compname"].ToString()

is giving an error saying "value of the type system.data.oledb.oledbdatareader cannot be converted to string type"..

Thank you again !!
Was This Post Helpful? 0
  • +
  • -

#4 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: Value from Database to textbox

Posted 26 September 2011 - 02:50 AM

IF oReader.HasRow THEN

Do While oReader.Read
textbox7.text = oReader(0)
Loop

ELSE
MsgBox("add new contact.")
END IF
Was This Post Helpful? 1
  • +
  • -

#5 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: Value from Database to textbox

Posted 26 September 2011 - 02:59 AM

It worked.. Thank you so very much, again !! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1