Cannot convbert from system.data.odbc/odbcConnection to string c#

mion shion 241 Reputation points
2022-08-21T12:12:17.35+00:00

i keep getting an error saying it can't convert to string and saying and cant understand why,

    private void InitializeDataGridView()    
    {  
        try  
        {  
            // Set up the DataGridView.  
            dataGridView1.Dock = DockStyle.Fill;  

            // Automatically generate the DataGridView columns.  
            dataGridView1.AutoGenerateColumns = true;  

            // Set up the data source.  
            bindingSource1.DataSource = GetData("Select * From shead");  
            dataGridView1.DataSource = bindingSource1;  

            // Automatically resize the visible rows.  
            dataGridView1.AutoSizeRowsMode =  
                DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;  

            // Set the DataGridView control's border.  
            dataGridView1.BorderStyle = BorderStyle.Fixed3D;  

            // Put the cells in edit mode when user enters them.  
            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;  
        }  
        catch (SqlException)  
        {  
            MessageBox.Show("To run this sample replace connection.ConnectionString" +  
                " with a valid connection string to a Northwind" +  
                " database accessible to your system.", "ERROR",  
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  
            System.Threading.Thread.CurrentThread.Abort();  
        }  
    }  

    private static DataTable GetData(string sqlCommand)  
    {  
        System.Data.Odbc.OdbcConnection connection =  
      new System.Data.Odbc.OdbcConnection("Driver={Microsoft Paradox Driver (*.db )};"  
                        + "DriverID=538;Fil=Paradox .X;"  
                        + "DefaultDir=C:\\Retcon\\Shifts\\;"  
                        + "Dbq=C:\\Retcon\\Shifts\\;"  
                        + "CollatingSequence=ASCII;");  
        System.Data.Odbc.OdbcCommand command =  
          new System.Data.Odbc.OdbcCommand();  


        using (SqlConnection northwindConnection = new SqlConnection(connection))  
        {  
            using (SqlCommand command = new SqlCommand(sqlCommand, northwindConnection))  
            {  
                SqlDataAdapter adapter = new SqlDataAdapter();  
                adapter.SelectCommand = command;  

                DataTable table = new DataTable();  
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;  
                adapter.Fill(table);  

                return table;  
            }  
        }  

    }  

but i get the following errors,

233253-screenshot-2022-08-21-131028.png

any help would be much appreciated as at a total loss to why its doing this.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,602 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,376 Reputation points
    2022-08-21T16:06:11.807+00:00

    See the following for a proper connection string and would recommend using OdbcConnectionStringBuilder.

    0 comments No comments

  2. Bruce (SqlWork.com) 60,866 Reputation points
    2022-08-21T16:07:46.61+00:00

    SqlConnection(connection) takes a string, not an OdbcConnection object.

    As the code only uses the SqlConnection, SqlCommand, it is not clear why you create an OdbcConnection() and OdbcCommand(). These are different libraries.

    SqlConnection() and SqlCommand() only support SqlServer queries.

    0 comments No comments

  3. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2022-08-23T07:31:55.647+00:00

    @mion shion , Welcome to Microsoft Q&A, as others suggested, we need to use the same type of connection to get datatable from sql database.

    You could try the following code to get the datatable by using the command.

     private static DataTable GetData(string sqlCommand)  
            {  
                System.Data.Odbc.OdbcConnection connection =  
              new System.Data.Odbc.OdbcConnection("Driver={Microsoft Paradox Driver (*.db )};"  
                                + "DriverID=538;Fil=Paradox .X;"  
                                + "DefaultDir=C:\\Retcon\\Shifts\\;"  
                                + "Dbq=C:\\Retcon\\Shifts\\;"  
                                + "CollatingSequence=ASCII;");  
                System.Data.Odbc.OdbcCommand command =  
                  new System.Data.Odbc.OdbcCommand(sqlCommand,connection);  
                System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter();  
                adapter.SelectCommand = command;  
                DataTable table = new DataTable();  
                adapter.Fill(table);  
                return table;  
      
            }  
    

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments