471,599 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

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

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.adOpenDyna mic,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

Nov 17 '05 #1
5 4500
Why are you using ADO in a .NET application?

"trint" <tr***********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.adOpenDyna mic,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

Nov 17 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"trint" <tr***********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.adOpenDyna mic,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

Nov 17 '05 #3
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

Nov 17 '05 #4
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"trint" <tr***********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 17 '05 #5
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

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David | last post: by
1 post views Thread by Trevor Fairchild | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.