ADO Record Set rs.MoveNext(); problem....help please

T

trint

Ok,
Everything connects correctly and loads properly in this
WebApplication. I have four buttons and all work correctly except one
(the NEXT RECORD button). PREVIOUS, FIRST and LAST all work properly.
This identical code works correctly in a windows app.
Please tell me what I am doing wrong.

Here is my connection code:

private void InitializeDataConnection ()
{

// frmStatus frmStatusMessage = new frmStatus();
if (!HasConnected)
{
// frmStatusMessage.Show("Connecting to SQL Server");

// Attempt to connect to SQL server or MSDE
bool IsConnecting = true;

while (IsConnecting)
{
try
{
// Open a Connection
cnn.ConnectionTimeout = 0;
cnn.CommandTimeout = 0;
cnn.Open(Connectionstring, null,null,0);

if (cnn.State != 1)
{
throw new System.Exception("Connection failed.");
}
if (!HasConnected)
{
// frmStatusMessage.Close();
}
IsConnecting = false;
HasConnected = true;
}
catch
{
if (Connectionstring == SQL_CONNECTION_STRING)
{
// Couldn't connect to SQL server. Now try MSDE
// Connectionstring = MSDE_CONNECTION_STRING;
// frmStatusMessage.Show("Connecting to MSDE");
}
else
{
// Unable to connect to SQL Server or MSDE
// frmStatusMessage.Close();

// MessageBox.Show("To run this sample you must have SQL
Server ot MSDE with the Northwind database installed. For instructions
on installing MSDE, view the Readme file." + MessageBoxIcon.Warning +
"SQL Server/MSDE not found");
// Quit program if neither connection method was successful.
// Application.Exit();
}
}
}
}
// }

// Build SQL statement.
// string strSQL = "SELECT * " +
// "FROM member " ;

string strSQL = "SELECT t1.member_id, t1.first_name, t1.last_name "
+
"FROM member t1 " ;

rs.ActiveConnection = cnn;
rs.CursorType = CursorTypeEnum.adOpenStatic;
rs.Open(strSQL,cnn,ADODB.CursorTypeEnum.adOpenDynamic,ADODB.LockTypeEnum.adLockOptimistic,0);
rs.MoveFirst();

}

and here is the code giving the problem:

private void Button2_Click(object sender, System.EventArgs e)
{
// this code is used to prevent going to EOF
if (!rs.EOF)
{
rs.MoveNext();
if (rs.EOF)
{
rs.MovePrevious();
}
PopulateSimpleNavigationForm();
}
}

Any help is appreciated.
Thanks,
Trint
 
N

Nicholas Paldino [.NET/C# MVP]

Trint,

I would replace that section with a check to the RecordCount property.
If the property is = 1 (meaning one record), then do nothing, otherwise,
call MoveNext.

Hope this helps.
 
T

trint

Nicholas,

ErrorLabel.Text = (rs.RecordCount.ToString()); is always -1
Does that mean that it's no longer open? But it moves with LAST
FIRST Previous?
I don't understand.
Thanks,
Trint
 
N

Nicholas Paldino [.NET/C# MVP]

Trint,

It's odd, you set the CursorType as static, then you open it with
dynamic. Unless you need to see changes that occur in the data as it
happens, I would change it to static. Because it is dynamic, I believe that
the recordset waits until you get to the end to calculate the record count.

I think that the cursor location has something to do with it as well. I
believe that if the cursor location is server, then the record count
property is affected as well.

All if this makes me very happy that there is one model for data access
in .NET (disconnected client side cursors).
 
T

trint

Nick,
I tried changing to "adOpenStatic" and it didn't change its
behavior...but, I think you are on to something
with what is wrong.
Thanks,
Trint
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top