Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:13:29+00:00 2026-06-10T13:13:29+00:00

Currently having an issue with this test windows form application. The constructor works to

  • 0

Currently having an issue with this test windows form application. The constructor works to add data to a datagridview. However, when I click the button, it fails to add anything to run the sql command with the error ExecuteNonQuery: Connection property has not been initialized. Below is my code. I have found questions with similar issues but they do not address this issue with calling the database at multiple points in the code.

namespace TestApplication
{
public partial class Form1 : Form
{
    public SqlDatabase db;
    string myConnectionString = ConfigurationManager.ConnectionStrings["DF06Data_Tools"].ToString();
    public Form1()
    {
        InitializeComponent();
        db = new SqlDatabase(myConnectionString);
        UpdateGrid();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void UpdateGrid()
    {
        string sql = "SELECT *  FROM [Data_Tools].[dbo].[TestTable]";
        DbCommand cmd = db.GetSqlStringCommand(sql);
        DataSet TableDataSet = db.ExecuteDataSet(cmd);
        dataGridView1.DataSource = TableDataSet.Tables[0];
        cmd.Dispose();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string sql = "INSERT INTO [Data_Tools].[dbo].[TESTTABLE] (name, id) VALUES('bob', '8')";
        DbCommand cmd = db.GetSqlStringCommand(sql);
        cmd.ExecuteNonQuery();
        UpdateGrid();
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:13:31+00:00Added an answer on June 10, 2026 at 1:13 pm

    You need to open your connection before calling ExecuteNonQuery. The call in your constructor opens the connection, issues the command, and then closes the connection for you.

    From MSDN:

    The call to ExecuteDataSet opens a connection, populates a DataSet,
    and closes the connection before returning the result.

    Also from MSDN:

    private static void CreateCommand(string queryString,
        string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(
                   connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using this jQuery validate plugin and having an issue in IE where
I am currently having an issue with BackgroundWorker running on Windows Server 2003. I
I am having this kind of mysterious issue here. I am currently using Entity
I'm currently having issue going through a test backup and restore of database table
I'm currently having an issue wrapping my brain around the notion of converting my
I'm currently having an issue storing mouse coordinates into an array in which a
Hi All I am currently having an issue calling a WCF service from a
I hope someone here can help me.. I am currently having a small issue
I'm having an issue with multiple markers on google maps - I currently have
i am having an issue, currently i am using cforms II and displaying a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.