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

  • Home
  • SEARCH
  • 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 8961889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:59:48+00:00 2026-06-15T15:59:48+00:00

I have started today to learn about the SQL connection in C# programming. And

  • 0

I have started today to learn about the SQL connection in C# programming. And I tried some basic thing like the Insert, Delete etc.

And I wanted to try the same thing in another project but I have a problem. Because It shows me an error that I can’t solve 🙁

Error:

A field initializer cannot reference the non-static field, method, or property ‘Artikujt.Form1.con’.

Here is my code (I just started to connect it with the Database in SQL)

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;

namespace Artikujt
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=TALY-PC;Initial Catalog=Katalogi;Integrated Security=True;Pooling=False");
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblArtikujt", con);
        DataSet dsl = new DataSet();    

        public Form1()
        {
            InitializeComponent();
        }    
    }
}

The error is in this code at the variable con

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblArtikujt", con);

I created the Database in SQL (in Visual Studio)… I also added the Data Source. I did the exact thing that I did before…. but it isn’t working 🙁

  • 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-15T15:59:49+00:00Added an answer on June 15, 2026 at 3:59 pm

    The error is exactly what it says. You cannot use con in your initialization of da because it will only be created during an instance creation. You will need to create con as static or put your initialization lines in your constructor.

    HOWEVER, you should probably not do this in practice as you are just asking for somebody to open a SqlConnection and leave it open. You should probably fall more into the practice of creating your connections using a using block to take advantage of the disposing pattern

    Static:

    public partial class Form1 : Form
    {
        static SqlConnection con = new SqlConnection(@"Data Source=TALY-PC;Initial Catalog=Katalogi;Integrated Security=True;Pooling=False");
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblArtikujt", con);
        DataSet dsl = new DataSet();    
    
    
        public Form1()
        {
            InitializeComponent();
        }
    

    Or instantiation in the constructor:

    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlDataAdapter da;
        DataSet dsl;  
    
        public Form1()
        {
            InitializeComponent();
            con = new SqlConnection(@"Data Source=TALY-PC;Initial Catalog=Katalogi;Integrated Security=True;Pooling=False");
            da = new SqlDataAdapter("SELECT * FROM tblArtikujt", con);
            dsl = new DataSet();  
        }
    }
    

    I will let you research the using block yourself

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

greetings, today i have started to learn java using netbeans ide. i would like
I started to learn about Mockito only today. I wrote some simple test (with
I've just (as in today) started working on some ZF stuff. I have a
I've started today with wordpress, I would like to know why do i have
Today I started work on a small Java app. I have some experience with
I just started to learn object oriented programming today and just by observation noticed
Today I started to learn iPhone simple animations, but I was totally Confused about
I have started to write some test cases today and I would really appreciate
I have started to do some programming using VIM. I have very mixed feelings
About a week ago I have started a online course compilers at coursera.org. Today

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.