-
September 8th, 2010, 11:35 AM
#1
Database connection issue
Hey all!
I'm rather new to connecting to external sources so bear with me. The error I'm getting is that "No data exists for the row/column."
the code in question:
String ConnectionString;
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source = C:\\Users;Extended Properties =dBase IV;";
System.Data.OleDb.OleDbConnection dBaseConnection;
dBaseConnection = new System.Data.OleDb.OleDbConnection(ConnectionString);
dBaseConnection.Open();
System.Data.OleDb.OleDbCommand dBaseCommand;
dBaseCommand = new System.Data.OleDb.OleDbCommand("SELECT FPHONE FROM ADDRESS", dBaseConnection);
System.Data.OleDb.OleDbDataReader dBaseDataReader;
dBaseDataReader = dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess);
txtFaxNum.Text = dBaseDataReader.GetValue(0).ToString();
The full data source is in that format (meaning seperated with \\ ) and the coloumn name is correct and that is the name of the table. Can anyone tell me what I'm doing wrong?
-
September 8th, 2010, 12:15 PM
#2
Re: Database connection issue
You need to call the OleDbDataReader.Read method before accessing the row data. Also, be sure to close the resources after you are finished with them. A 'using' block is a handy way to do this.
Code:
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source = C:\\Users;Extended Properties =dBase IV;";
using( var dBaseConnection = new OleDbConnection(connectionString) )
{
dBaseConnection.Open();
using( OleDbCommand dBaseCommand = new OleDbCommand("SELECT FPHONE FROM ADDRESS", dBaseConnection) )
{
OleDbDataReader dBaseDataReader = dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess);
while( dBaseDataReader.Read( ) )
{
// retrieve the string from column 0 (NOTE: if the query returns
// more than one record, all faxnum will be overwritten except
// the last one
txtFaxNum.Text = dBaseDataReader.GetString( 0 );
}
}
}
-
September 8th, 2010, 12:25 PM
#3
Re: Database connection issue
Okay, but now it tells me the cast is invalid on this line:
txtFaxNum.Text = dBaseDataReader.GetString(0);
I can't think straight anymore, I've been staring at this program all day and I keep having to come back to this bit.
-
September 8th, 2010, 01:57 PM
#4
Re: Database connection issue
How about textbox1.txt = dBaseDataReader("whateverColum").ToString ?
-
September 8th, 2010, 03:16 PM
#5
Re: Database connection issue
Originally Posted by WileeDarklight
Okay, but now it tells me the cast is invalid on this line:
txtFaxNum.Text = dBaseDataReader.GetString(0);
I can't think straight anymore, I've been staring at this program all day and I keep having to come back to this bit.
Yeah, sorry - I use a safe data reader class that's slightly different. It should be:
Code:
txtFaxNum.Text = dBaseDataReader[ 0 ].GetString( );
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|