adding rows to gridview
Posted on 2010-09-22
This should be really simple.... but the best-practice solution is escaping me
ASP.net - Visual Basic - Code Behind
I have an input field - the user enters a user-id and clicks a lookup button.
The click event looks up the user in a database and writes a row to a visible gridview.
EACH time the user clicks the lookup button a NEW/ADDITIONAL row is added to the gridview.
What I currently have in my lookup button click code is
1. Make sure the user input is valid
2. build the sql string using the user input value
3. Use an OracleDataReader to hold the returned data
4. Bind a gridview to the datareader
The problem is that each time the lookup button is clicked the gridview will re-bind to the datareader that only has a single most recent record in it. But I want the gridview to grow each time the user clicks the lookup button. I tried using an intermediary datable but ran into the same problem - I couldn't manage to create anything that was growing at each click rather than overwriting.
What is the best way to go about this? actual code would be most handy - here is a bit of my click code:
If (FoundError = False) Then
' lookup name and ssn in database and add the data to our datagrid
Dim MyOracleConnectionString As String = ConfigurationManager.ConnectionStrings("MyOracleConnection").ConnectionString
Using MyOracleConnection As New OracleConnection (MyOracleConnectionString)
Using MyCommand As New OracleCommand
MyCommand.CommandText = "select bla bla bla "
MyCommand.Connection = MyOracleConnection
' use a data-reader when using sql to return multiple columns
Using MyDataReader As OracleDataReader = MyCommand.ExecuteReader()
If (MyDataReader.HasRows) then
Do While MyDataReader.Read()
' Add the datareader row to the datatable
MyDataRow = MyDataTable.NewRow
MyDataRow("StuID") = MyDataReader("StuID")
MyDataRow("StuName") = MyDataReader("StuName")
MyDataRow("StuSSN") = MyDataReader("StuSSN")
' bind the datatable to the gridview
X_OutputSection_GridView.DataSource = MyDataTable