hello guys
what is the best way of populating text boxes with values from the database. I am using access and want to display some values to two textboxes . I have been using the GetData method but I got a prpblem , in case the value is not found I want to call another form where it can be added to the database. the data being got is from the same table.
thanks.

Hi

Can you post the code that you are currently using? Usually, if you are just looking to grab some data I would recommend using an OleDbDataReader which can be implemented along the lines of:

Dim connectionString As String = "Your connection string"
Dim connection As New OleDbConnection(connectionString)

Dim command As New OleDbCommand("SELECT Fields FROM Table", connection)

connection.Open()

Dim reader As OleDbDataReader = command.ExecuteReader

If reader.HasRows

    reader.Read()
    TextBox1.Text = reader("FieldName").ToString
    TextBox2.Text = reader("FieldName").ToString

End If

connection.Dispose()

The above assumes that you are getting a single record back from the table.

HTH

hello
sorry for late response. here is my code.

dim em as new emmatableadpter.itemtableadapter
dim et = em.getdatabyitemname(textbox1.text)
if et.rows.count=0
    textbox2.text=""

    dim x as Emma.emmadataset.item=et.rows
    textbox2.text=x.price

its something like that. the code is able to display if the item exists, but if no item is found the text box remains empty. but I don't want it to remain empty. I want to call a windows form where it can be included into the database.
thanks.

Hi

If you want to show a form when there is no data then you would do something like:

If et.rows.count = 0 Then
    Dim addForm as New YourOtherForm
    addForm.Show 'Or use ShowDialog to keep it on top until closed
Else
    TextBox2.Text = x.Price
End If

HTH

hi
I tried that out earlier but I didn't use the Else and it kept on loading the AddForm after every letter I typed. do u hope the Else statement will solve that.

thanks.

It should but not having seen the full code I can't say for certain. Why don't you post your entire routine that handles this as that will make it much easier to assist.

hi
here it is.

Dim EA as new RetailDataSetTableAdapters.itemTableAdapter

Dim EB = EA.GetDataByitemname(text box1. Text)

if EB.Rows.Count=0

    textbox2.text=""

    Button1.Enabled=False

    exit sub

end if

button1.enabled=true

dim X as Retail.RetailDataSet.item row=EB.Rows(0)

textbox2.text=X.price

that is the code to display but if nothing is found, I want to call AddForm.

thanks.

i don't know if it help or not but i use this to fill textbox from datagridview by cellclick event

textbox1.Text = dgv.Rows(e.RowIndex).Cells(0).Value.ToString()
textbox2.Text = dgv.Rows(e.RowIndex).Cells(1).Value.ToString()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.