0

i am new in vb.net , i have already displayed data from database into textbox , the problem if the result of search more than one only the last record are displayed i want to use up down keys in that textbox to explore all data . thanks ;;

    con = New SqlConnection(cs)
        con.Open()
        Dim sql As String = " Select RTRIM(visit.regdate),RTRIM(Patientno) from visit where visit.accno =@d6 "
        cmd = New SqlCommand(sql, con)
        cmd.Parameters.AddWithValue("d6", accno.Text)
        rdr = cmd.ExecuteReader()




        While (rdr.Read() = True)


            regdate.Value = rdr.GetValue(0)
            patientno.Text = rdr.GetValue(1)


        End While
  • You should load the data into a `DataTable` from the data reader, bind that to a `BindingSource` and bind that to the `TextBox`. You can then call the `MoveNext` and `MovePrevious` methods of the `BindingSource` to navigate. – jmcilhinney Jul 18 '17 at 12:35
  • a dropdown is more appropriate OR grid than a textbox. bind your datatable to the dropdown and then you do not need to write any weird code to get your arrow keys to function for scrolling the data bound to the dropdown – Aaron. S Sep 19 '17 at 18:33

1 Answers1

0

One of the better ways to display data use to use the data table control. It is very simple to use. With the Datatable control, you can show all records you want to display, but with added code, also have the ability to update, delete, filter, and search.

using cmd as new sqlcommand("sql string",sqlconnection)
cmd.parameter.add(new sqlparameter("@parameter",parametervalue))

'Then create a Sql data adapter and data table variable
 Dim myAdapter as new Sqldataadapter(cmd)
 Dim dt as new Datatable

'fill the datatable variable, best to catch it in a try catch statement
try
  myAdapter.Fill(dt)
catch ex as exception
 throw new exception(ex.tostring)
end try

'then assign the tables datasource, with the recently filled datatable
 DatatableControl.Datasource = dt

 'end using statment
 End Using
AustinS90
  • 153
  • 6
  • throwing a new exception is not the correct way. just use throw. this will keep your inner exception from being lost. https://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c AND https://stackoverflow.com/questions/22623/best-practices-for-catching-and-re-throwing-net-exceptions – Aaron. S Sep 19 '17 at 19:18