locked
How to Fix "The Connection String Property has not initialized RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            System.Data.SqlClient.SqlConnection con;
            DataSet ds1;
            System.Data.SqlClient.SqlDataAdapter da;

            int MaxRows = 0;
            int inc = 0;

            private void Form1_Load(object sender, EventArgs e)
            {

                    con = new System.Data.SqlClient.SqlConnection();


                    con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Arshad\\Documents\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                    ds1 = new DataSet();

                    string sql = "SELECT * From tblWorkers";
                    da = new System.Data.SqlClient.SqlDataAdapter(sql, con);

                    con.Open();

                    da.Fill(ds1, "Workers");
                    NavigateRecords();
                    MaxRows = ds1.Tables["Workers"].Rows.Count;



                    con.Close();
                 //   MessageBox.Show("DataBase Closed");
                    con.Dispose();


            }

            private void NavigateRecords()
            {
                DataRow dRow = ds1.Tables["Workers"].Rows[inc];

                textBox1.Text = dRow.ItemArray.GetValue(1).ToString();
                textBox2.Text = dRow.ItemArray.GetValue(2).ToString();
                textBox3.Text = dRow.ItemArray.GetValue(3).ToString();

            }

            private void btnNext_Click(object sender, EventArgs e)
            {
                if (inc != MaxRows - 1)
                {
                    inc++;
                    NavigateRecords();
                }
                else
                {
                    MessageBox.Show("No More Records");
                }
            }

            private void btnPrevious_Click(object sender, EventArgs e)
            {
                if (inc > 0)
                {
                    inc--;
                    NavigateRecords();
                }
                else
                {
                    MessageBox.Show("First Record");
                }
            }

            private void btnFirst_Click(object sender, EventArgs e)
            {
                if (inc != 0)
                {
                    inc = 0;
                    NavigateRecords();
                }
            }

            private void btnLast_Click(object sender, EventArgs e)
            {
                if (inc != MaxRows - 1)
                {
                    inc = MaxRows - 1;
                    NavigateRecords();
                }
            }

            private void btnNew_Click(object sender, EventArgs e)
            {
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
            }

             private void btnSave_Click(object sender, EventArgs e)
            {

                System.Data.SqlClient.SqlCommandBuilder cb;
                cb = new System.Data.SqlClient.SqlCommandBuilder(da);

                DataRow dRow = ds1.Tables["Workers"].NewRow();

                dRow[1] = textBox1.Text;
                dRow[2] = textBox2.Text;
                dRow[3] = textBox3.Text;


                ds1.Tables["Workers"].Rows.Add(dRow);

                MaxRows = MaxRows + 1;
                inc = MaxRows - 1;


                da.Update(ds1, "Workers");
                MessageBox.Show("Entry Added");

            }


        }
    }

    Respected sir whenever i run this program all buttons works properly but when i want to add new record and save it then this type of exeption occurs:

    An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

    Additional information: The ConnectionString property has not been initialized.

     

                                                                                                                                                       
    • Edited by ArshadMaliik Tuesday, September 18, 2012 7:07 PM
    Tuesday, September 18, 2012 6:53 PM

Answers

  • You should probably create an instance of SqlConnection, with your connection string and open this connection before try to make any command.

    SqlConnection con = new SqlConnection("myConnectionstring");


    Web Developer

    • Marked as answer by ArshadMaliik Tuesday, September 18, 2012 7:07 PM
    • Unmarked as answer by ArshadMaliik Tuesday, September 18, 2012 7:07 PM
    • Marked as answer by ArshadMaliik Monday, October 15, 2012 10:55 AM
    Tuesday, September 18, 2012 6:57 PM
  • You are missing these lines in your btnSave_Click handler.

    con = new System.Data.SqlClient.SqlConnection();
    con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Arshad\\Documents\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

    con.Open();


    These are the same lines you used before. You might only need con.Open(); if you delete the con.Dispose(); in your Form1_Load handler.


    Dan Randolph - My Code Samples List

    Tuesday, September 18, 2012 9:06 PM
  • move the line

    con = new system.data......

    so that it is global - move to between the class and the public form1()

    Now con will be available throughout your code.

    Also, are you sure you have bound the datagrid to the source?


    Digital Forensic Software Developer
    CCS LABS Digital Forensic Software
    Mark as Answer or Vote up if useful thank you!

    • Marked as answer by ArshadMaliik Monday, October 15, 2012 10:55 AM
    Friday, September 21, 2012 8:33 AM

All replies

  • You should probably create an instance of SqlConnection, with your connection string and open this connection before try to make any command.

    SqlConnection con = new SqlConnection("myConnectionstring");


    Web Developer

    • Marked as answer by ArshadMaliik Tuesday, September 18, 2012 7:07 PM
    • Unmarked as answer by ArshadMaliik Tuesday, September 18, 2012 7:07 PM
    • Marked as answer by ArshadMaliik Monday, October 15, 2012 10:55 AM
    Tuesday, September 18, 2012 6:57 PM
  • You are missing these lines in your btnSave_Click handler.

    con = new System.Data.SqlClient.SqlConnection();
    con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Arshad\\Documents\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

    con.Open();


    These are the same lines you used before. You might only need con.Open(); if you delete the con.Dispose(); in your Form1_Load handler.


    Dan Randolph - My Code Samples List

    Tuesday, September 18, 2012 9:06 PM
  • move the line

    con = new system.data......

    so that it is global - move to between the class and the public form1()

    Now con will be available throughout your code.

    Also, are you sure you have bound the datagrid to the source?


    Digital Forensic Software Developer
    CCS LABS Digital Forensic Software
    Mark as Answer or Vote up if useful thank you!

    • Marked as answer by ArshadMaliik Monday, October 15, 2012 10:55 AM
    Friday, September 21, 2012 8:33 AM