An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll Additional information: The ConnectionString property has not been initialized.

Apex6554 60 Reputation points
2023-04-04T19:14:56.5566667+00:00

I've looked everywhere on Google to find a fix but I can't a single fix, so. I'm just gonna ask and post my code here and see if anyone can find my problem.

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;
using System.Data.SqlClient;
using System.Configuration;

namespace Apex6554_MW3_RTM_Tool
{
    public partial class Registration : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-S0G9CIH\APEX6654;Initial Catalog=tbl_Register;Integrated Security=True;Pooling=False");
        private string connectionString;

        public Registration()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            Form1 back = new Form1();
            back.Show();
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlCon = new SqlConnection(connectionString))
            {
                if (txtUsername.Text == "" || txtPassword.Text == "")
                    MessageBox.Show("Please fill mandatory fields");
                sqlCon.Open();
                SqlCommand sqlCmd = new SqlCommand("UserAdd", sqlCon);
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
                sqlCmd.ExecuteNonQuery();
                MessageBox.Show("Registration Succesfully !");
                Clear();
            }
        }
        void Clear()
        {
            txtEmail.Text = txtUsername.Text = txtPassword.Text = "";
        }
    }
}
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,899 questions
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,390 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 113K Reputation points
    2023-04-04T19:38:29.9166667+00:00

    Try this adjustment:

    public partial class Registration : Form
    {
       string connectionString = @"Data Source=DESKTOP-S0G9CIH\APEX6654;Initial Catalog=tbl_Register;Integrated Security=True;Pooling=False";
    
       public Registration()
       {
    . . .
    
    0 comments No comments

0 additional answers

Sort by: Most helpful