Equivalent of the .MoveFirst etc. in a DataSet

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am sure this is looking me in the face, but I've stared at it so long I
can't see the forest anymore because of all the trees. This should be simple.

I want to populate a DataGrid with the results obtained from a SQL database
that is located in a DataSet. I am also using the same results to populate a
listbox for the user to select a specific functionality.

If I populate the DataGrid first, the listbox won't populate, and vise
versa. I know it's because the DataSet is at EOF. What command do I use to
move back to the first record in the DataSet (like the old rs.MoveFirst), and
for that matter, what are the commands (like the rs.move xxx) that allow
traversing the rows?

Any help would be really appreciated. Thanks in advance.

Jim Hawley
 
Hi Jim,

I recon you are NOT using databinding.
Can you post a sample of your code to show us how you want to fill your
DataGrid and ListBox?

Christiaan
 
Am using DataBind for both. Here is the code:

sub Page_Load(Sender as Object, e as EventArgs)
if not Page.IsPostBack then
dim SelectCommand As New SqlClient.SqlCommand(sSproc, Conn)
dim objReader as SqlDataReader
try
SelectCommand.Connection.Open()
objReader = SelectCommand.ExecuteReader()
do While objReader.Read()
test_number.Items.Add(objReader("test_name"))
test_number.DataBind()
loop
DataGrid1.DataSource = objReader
DataGrid1.DataBind()
catch ex as SqlException
lblMsg.Text = ex.Message
finally
SelectCommand.Connection.Close()
end try
end if
end sub

sSproc is the name of the stored procedure being called.

Thanks in advance.

Jim Hawley
 
DJ said:
Am using DataBind for both. Here is the code:

sub Page_Load(Sender as Object, e as EventArgs)
if not Page.IsPostBack then
dim SelectCommand As New SqlClient.SqlCommand(sSproc, Conn)
dim objReader as SqlDataReader
try
SelectCommand.Connection.Open()
objReader = SelectCommand.ExecuteReader()
do While objReader.Read()
test_number.Items.Add(objReader("test_name"))
test_number.DataBind()
loop
DataGrid1.DataSource = objReader
DataGrid1.DataBind()
catch ex as SqlException
lblMsg.Text = ex.Message
finally
SelectCommand.Connection.Close()
end try
end if
end sub

You are using a DataReader, which is "forward only". Don't do that. Use a
DataSet instead.

David
 
Jim,

Try this:

sub Page_Load(Sender as Object, e as EventArgs)
if not Page.IsPostBack then
dim SelectCommand As New SqlClient.SqlCommand(sSproc, Conn)
dim objDataSet as DataSet
try
dim objDataAdapter as New DataAdapter(SelectCommand)
objDataAdapter.Fill(objDataSet)
catch ex as SqlException
lblMsg.Text = ex.Message
finally
Conn.Close()
end try
DataGrid1.DataSource = objDataSet
DataGrid1.DataMember = objDataSet.Tables[0].TableName // <=
only when you do not know the tablename
DataGrid1.DataBind()

// bind your listbox here on the DataSet too

end if

end sub

(Code is not tested, but should work)

HTH
Christiaan
 
Thanks, appreciate it.

Jim

Christiaan van Bergen said:
Jim,

Try this:

sub Page_Load(Sender as Object, e as EventArgs)
if not Page.IsPostBack then
dim SelectCommand As New SqlClient.SqlCommand(sSproc, Conn)
dim objDataSet as DataSet
try
dim objDataAdapter as New DataAdapter(SelectCommand)
objDataAdapter.Fill(objDataSet)
catch ex as SqlException
lblMsg.Text = ex.Message
finally
Conn.Close()
end try
DataGrid1.DataSource = objDataSet
DataGrid1.DataMember = objDataSet.Tables[0].TableName // <=
only when you do not know the tablename
DataGrid1.DataBind()

// bind your listbox here on the DataSet too

end if

end sub

(Code is not tested, but should work)

HTH
Christiaan





DJ said:
Am using DataBind for both. Here is the code:

sub Page_Load(Sender as Object, e as EventArgs)
if not Page.IsPostBack then
dim SelectCommand As New SqlClient.SqlCommand(sSproc, Conn)
dim objReader as SqlDataReader
try
SelectCommand.Connection.Open()
objReader = SelectCommand.ExecuteReader()
do While objReader.Read()
test_number.Items.Add(objReader("test_name"))
test_number.DataBind()
loop
DataGrid1.DataSource = objReader
DataGrid1.DataBind()
catch ex as SqlException
lblMsg.Text = ex.Message
finally
SelectCommand.Connection.Close()
end try
end if
end sub

sSproc is the name of the stored procedure being called.

Thanks in advance.

Jim Hawley
 
Back
Top