Question The ConnectionString property has not been initialized

andrewmanuja

Well-known member
Joined
May 30, 2019
Messages
75
Programming Experience
Beginner
Hi All,
I am running a WindowForm application and got a dataGridView where I add a new column(type of boolean) into it, during the Form Load event to select the data rows.
The respective code is as follows;
C#:
//Add column for selection
                table.Columns.Add("Select Item", typeof(bool));

                //Bind to dataGrid
                dataGridViewOrderDetails.DataSource = table;
                dataGridViewOrderDetails.Columns["Select Item"].DisplayIndex = 0;

I have create the connection string in the "App.Config file and calling the connection as follows;
C#:
SqlConnection con = Connection.GetConnection();

I have also created a DataTable() within the public class of the window form.
C#:
private DataTable table = new DataTable();

Basically, my DataGridView will have multiple data records and user will select a few and once hit the "Transfer" button, few table in my SQL Server need to be updated accordingly.
The code is as follows;
C#:
  table.AcceptChanges();
            //Mark selected rows ready for insert
            foreach (DataRow row in table.Rows)
            {
                if (row["Select Item"] as bool? == true)
                {
                    row.SetAdded();
                }
            }

            //Save Data to Destination

            //Below section add the respective drug quantities to the respective cost centre drug available quantity
        
            using (var command = new SqlCommand(@"UPDATE tbl_CostCentreDrugStock SET tbl_CostCentreDrugStock.availableQuantity += @aissueQuantity
                                                            FROM tbl_CostCentreDrugStock
                                                            INNER JOIN tbl_CostCentreOrderStatus
                                                            ON tbl_CostCentreDrugStock.costCentreID = tbl_CostCentreOrderStatus.costCentreID
                                                            WHERE tbl_CostCentreOrderStatus.ccOrderID =@accOrderID AND tbl_CostCentreDrugStock.drugID =@adrugID ", con))

            using (var adapter = new SqlDataAdapter { InsertCommand = command })
            {
                command.Parameters.Add("@adrugID", SqlDbType.Int, 0, "drugID");
                command.Parameters.Add("@accOrderID", SqlDbType.Int, 0, "ccOrderID");
                command.Parameters.Add("@aissueQuantity", SqlDbType.Int, 0, "issueQuantity");

                try
                {
                    adapter.Update(table);
                    MessageBox.Show("Data Saved");
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
                command.Parameters.Clear();
            }
            
            //Below section deduct the respective drug quantities from the master drug table
            using (var command = new SqlCommand(@"UPDATE tbl_DrugQuantity SET availableQuantity -= @aissueQuantity
                                                            WHERE drugID = @adrugID", con))

            using (var adapter = new SqlDataAdapter { InsertCommand = command })
            {
                command.Parameters.Add("@adrugID", SqlDbType.Int, 0, "drugID");
                command.Parameters.Add("@aissueQuantity", SqlDbType.Int, 0, "issueQuantity");

                try
                {
                    adapter.Update(table);
                    MessageBox.Show("Data Saved");
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
                command.Parameters.Clear();
            }
I may be doing a silly mistake here. However, when I run the program and hit the Transfer button, I am getting the error, "The ConnectionString property has not been initialized".

Appreciate your valuable feedback please.

Kind regards,

Andrew
 
Back
Top Bottom