Database connection issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Database connection issue

  1. #1
    Join Date
    Sep 2010
    Posts
    24

    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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    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 );
        }
      }
    }

  3. #3
    Join Date
    Sep 2010
    Posts
    24

    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.

  4. #4
    Join Date
    Dec 2009
    Posts
    596

    Re: Database connection issue

    How about textbox1.txt = dBaseDataReader("whateverColum").ToString ?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    Re: Database connection issue

    Quote Originally Posted by WileeDarklight View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center