none
"The ConnectionString property has not been initialized." Error for DataAdapter.Fill() in C# RRS feed

  • Question

  • I'm trying to  add  a Crystal Report Viewer  in Windows form but it shows error for DataAdapter.Fill() 

    This is my code:

    namespace Trying
    {
        public partial class Form2 : Form
        {
            OleDbConnection cn = new OleDbConnection();
            public Form2()
            {
                InitializeComponent();
            }
            private void Form2_Load(object sender, EventArgs e)
            {
            }
            private void button1_Click(object sender, EventArgs e)
            {
                ReportDocument Cryrpt = new ReportDocument();
                DataSet ds = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter("Select * from Table1 Where [Name] ='" + textBox1.Text + "'",cn);
               
                da.Fill(ds,"Table1");
                Cryrpt.Load(@"E:\\New Volume (E)\\sem6 project\\Trying\\Trying\\Reports\\CrystalReport1.rpt");
                Cryrpt.SetDataSource(ds);
                crystalReportViewer1.ReportSource = Cryrpt;
                ds.Dispose();

            }

    }

    }

    Saturday, March 21, 2020 4:19 PM

All replies

  • Hi Student Developer Jas,

    It seems you didn't open the connection (  cn.Open(); ) and you haven't any connection string for a database, so I rewrite your code and I have test it every thins went fine,

     public partial class Form2 : Form
        {
    
    
           private OleDbConnection cn;
    
    
            public Form2()
            {
                InitializeComponent();
    
                var constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabaseTest.accdb";
                cn = new OleDbConnection(constr);
    
           
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                try
                {
                    cn.Open();
    
                    DataSet ds = new DataSet();
                    OleDbDataAdapter da = new OleDbDataAdapter("Select * from tblStudent Where [Name] ='" + textBox1.Text + "'", cn);
    
                    da.Fill(ds, "Table1");
    
    
                    Cryrpt.Load(@"E:\\New Volume (E)\\sem6 project\\Trying\\Trying\\Reports\\CrystalReport1.rpt");
                    Cryrpt.SetDataSource(ds);
                    crystalReportViewer1.ReportSource = Cryrpt;
                    
    
    
                    ds.Dispose();
    
    
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
    
                finally
                {
    
    
                    cn.Close();
                    cn.Dispose();
    
    
    
                }
    
    
    
    
    
    
    
    
    
                
            }
        }


    Please remember to mark the replies as answers if they helped you :) ~


    • Edited by Rebin Qadir Saturday, March 21, 2020 8:49 PM correction text
    Saturday, March 21, 2020 8:04 PM
  • It seems you didn't open the connection (  cn.Open(); )

    No, this is not necessary. Contrary to the DataReader, which does require an open connection, the DataAdapter doesn't. When you call the Fill method, if it finds that the connection is closed then it automatically opens the connection, uses it, and then closes it. So you can save yourself from writing the open and using a try...finally to close the connection at the end. This is needed if you are using a DataReader, but it si not needed for a DataAdapter.

    On the other hand, the missing connection string is the right answer to the original problem. The program will not work if you do this:

    OleDbConnection cn = new OleDbConnection();

    You need to do this:

    OleDbConnection cn = new OleDbConnection("Write here your connection string");

    You need to have knowledge about your database in order to write the part that says "Write here your connection string". The program cannot magically determine where is your data and how to access it unless you give it all the details there.

    Sunday, March 22, 2020 10:08 AM
    Moderator
  • No, this is not necessary. Contrary to the DataReader, which does require an open connection, the DataAdapter doesn't. When you call the Fill method, if it finds that the connection is closed then it automatically opens the connection, uses it, and then closes it. So you can save yourself from writing the open and using a try...finally to close the connection at the end. This is needed if you are using a DataReader, but it si not needed for a DataAdapter.

       

     I have updated the code depending on Alberto Poblacion's answer, just insert the connection string of the database.

     public partial class Form2 : Form
        {
    
    
           private OleDbConnection cn;
    
    
            public Form2()
            {
                InitializeComponent();
    
                var constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabaseTest.accdb";
                cn = new OleDbConnection(constr);
    
            
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                try
                {
                  
    
                    DataSet ds = new DataSet();
                    OleDbDataAdapter da = new OleDbDataAdapter("Select * from tblStudent Where [Name] ='" + textBox1.Text + "'", cn);
    
                    da.Fill(ds, "Table1");
    
    
                    Cryrpt.Load(@"E:\\New Volume (E)\\sem6 project\\Trying\\Trying\\Reports\\CrystalReport1.rpt");
                    Cryrpt.SetDataSource(ds);
                    crystalReportViewer1.ReportSource = Cryrpt;
                     
    
                    ds.Dispose();
    
    
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
    
                
    
    
    
    
    
    
    
    
                
            }
        }


    Please remember to mark the replies as answers if they helped you :) ~


    • Edited by Rebin Qadir Sunday, March 22, 2020 11:53 AM
    Sunday, March 22, 2020 11:53 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 27, 2020 9:30 AM