Connection property has not been initialized - Npgsql C#










-1















I am trying to connect to my postgres database but im getting stuck with Connection property has not been initialized.



I am using a class for open connection



 class DBConnection

public static NpgsqlConnection conn = null;

public void Connection_open()

string connstring = String.Format("Host=localhost;Database=Dokument_API;Username=postgres;Password=******");
var conn = new NpgsqlConnection(connstring);
conn.Open();

public void Connection_close()

conn.Close();




In form I have this code



private void button1_Click(object sender, EventArgs e)

//Open connection from class
DBConnection NewConnection = new DBConnection();
NewConnection.Connection_open();

NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO anrop (exempelanrop,beskrivning,exempelsvar) VALUES ('" + exempelanrop_text.Text + "','" + beskrivning_text.Text + "','" + exempelsvar_text.Text + "')", DBConnection.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Uppgifter sparade");



I get stuck on cmd.ExecuteNonQuery. Why??










share|improve this question

















  • 2





    DBConnection is a bug. You shouldn't have a global connection. A connection should be opened right before it's used and closed as soon as possible. Locks taken while executing queries remain until a connection is closed. This means they end up blocking other connections.

    – Panagiotis Kanavos
    Nov 13 '18 at 15:16












  • Everytime you are opening the connection, where is your connection closing also you have used var conn which will neglect the static conn

    – Hassaan
    Nov 13 '18 at 15:18











  • If you want a generic, reusable DbConnection abstraction just use the classes already provided by ADO.NET. All ADO.NET provider classes inherit from abstract classes like DbConnection, DbCommand, DbDataReader

    – Panagiotis Kanavos
    Nov 13 '18 at 15:18











  • If you worry that opening and closing a connection is costly, don't. Most ADO.NET providers implement connection pooling. When .Dispose() or .Close() is called on a connection, it's reset and put in a connection pool, ready for reuse.

    – Panagiotis Kanavos
    Nov 13 '18 at 15:19















-1















I am trying to connect to my postgres database but im getting stuck with Connection property has not been initialized.



I am using a class for open connection



 class DBConnection

public static NpgsqlConnection conn = null;

public void Connection_open()

string connstring = String.Format("Host=localhost;Database=Dokument_API;Username=postgres;Password=******");
var conn = new NpgsqlConnection(connstring);
conn.Open();

public void Connection_close()

conn.Close();




In form I have this code



private void button1_Click(object sender, EventArgs e)

//Open connection from class
DBConnection NewConnection = new DBConnection();
NewConnection.Connection_open();

NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO anrop (exempelanrop,beskrivning,exempelsvar) VALUES ('" + exempelanrop_text.Text + "','" + beskrivning_text.Text + "','" + exempelsvar_text.Text + "')", DBConnection.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Uppgifter sparade");



I get stuck on cmd.ExecuteNonQuery. Why??










share|improve this question

















  • 2





    DBConnection is a bug. You shouldn't have a global connection. A connection should be opened right before it's used and closed as soon as possible. Locks taken while executing queries remain until a connection is closed. This means they end up blocking other connections.

    – Panagiotis Kanavos
    Nov 13 '18 at 15:16












  • Everytime you are opening the connection, where is your connection closing also you have used var conn which will neglect the static conn

    – Hassaan
    Nov 13 '18 at 15:18











  • If you want a generic, reusable DbConnection abstraction just use the classes already provided by ADO.NET. All ADO.NET provider classes inherit from abstract classes like DbConnection, DbCommand, DbDataReader

    – Panagiotis Kanavos
    Nov 13 '18 at 15:18











  • If you worry that opening and closing a connection is costly, don't. Most ADO.NET providers implement connection pooling. When .Dispose() or .Close() is called on a connection, it's reset and put in a connection pool, ready for reuse.

    – Panagiotis Kanavos
    Nov 13 '18 at 15:19













-1












-1








-1








I am trying to connect to my postgres database but im getting stuck with Connection property has not been initialized.



I am using a class for open connection



 class DBConnection

public static NpgsqlConnection conn = null;

public void Connection_open()

string connstring = String.Format("Host=localhost;Database=Dokument_API;Username=postgres;Password=******");
var conn = new NpgsqlConnection(connstring);
conn.Open();

public void Connection_close()

conn.Close();




In form I have this code



private void button1_Click(object sender, EventArgs e)

//Open connection from class
DBConnection NewConnection = new DBConnection();
NewConnection.Connection_open();

NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO anrop (exempelanrop,beskrivning,exempelsvar) VALUES ('" + exempelanrop_text.Text + "','" + beskrivning_text.Text + "','" + exempelsvar_text.Text + "')", DBConnection.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Uppgifter sparade");



I get stuck on cmd.ExecuteNonQuery. Why??










share|improve this question














I am trying to connect to my postgres database but im getting stuck with Connection property has not been initialized.



I am using a class for open connection



 class DBConnection

public static NpgsqlConnection conn = null;

public void Connection_open()

string connstring = String.Format("Host=localhost;Database=Dokument_API;Username=postgres;Password=******");
var conn = new NpgsqlConnection(connstring);
conn.Open();

public void Connection_close()

conn.Close();




In form I have this code



private void button1_Click(object sender, EventArgs e)

//Open connection from class
DBConnection NewConnection = new DBConnection();
NewConnection.Connection_open();

NpgsqlCommand cmd = new NpgsqlCommand("INSERT INTO anrop (exempelanrop,beskrivning,exempelsvar) VALUES ('" + exempelanrop_text.Text + "','" + beskrivning_text.Text + "','" + exempelsvar_text.Text + "')", DBConnection.conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Uppgifter sparade");



I get stuck on cmd.ExecuteNonQuery. Why??







c# npgsql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 15:12









Daniel StrömbergDaniel Strömberg

72




72







  • 2





    DBConnection is a bug. You shouldn't have a global connection. A connection should be opened right before it's used and closed as soon as possible. Locks taken while executing queries remain until a connection is closed. This means they end up blocking other connections.

    – Panagiotis Kanavos
    Nov 13 '18 at 15:16












  • Everytime you are opening the connection, where is your connection closing also you have used var conn which will neglect the static conn

    – Hassaan
    Nov 13 '18 at 15:18











  • If you want a generic, reusable DbConnection abstraction just use the classes already provided by ADO.NET. All ADO.NET provider classes inherit from abstract classes like DbConnection, DbCommand, DbDataReader

    – Panagiotis Kanavos
    Nov 13 '18 at 15:18











  • If you worry that opening and closing a connection is costly, don't. Most ADO.NET providers implement connection pooling. When .Dispose() or .Close() is called on a connection, it's reset and put in a connection pool, ready for reuse.

    – Panagiotis Kanavos
    Nov 13 '18 at 15:19












  • 2





    DBConnection is a bug. You shouldn't have a global connection. A connection should be opened right before it's used and closed as soon as possible. Locks taken while executing queries remain until a connection is closed. This means they end up blocking other connections.

    – Panagiotis Kanavos
    Nov 13 '18 at 15:16












  • Everytime you are opening the connection, where is your connection closing also you have used var conn which will neglect the static conn

    – Hassaan
    Nov 13 '18 at 15:18











  • If you want a generic, reusable DbConnection abstraction just use the classes already provided by ADO.NET. All ADO.NET provider classes inherit from abstract classes like DbConnection, DbCommand, DbDataReader

    – Panagiotis Kanavos
    Nov 13 '18 at 15:18











  • If you worry that opening and closing a connection is costly, don't. Most ADO.NET providers implement connection pooling. When .Dispose() or .Close() is called on a connection, it's reset and put in a connection pool, ready for reuse.

    – Panagiotis Kanavos
    Nov 13 '18 at 15:19







2




2





DBConnection is a bug. You shouldn't have a global connection. A connection should be opened right before it's used and closed as soon as possible. Locks taken while executing queries remain until a connection is closed. This means they end up blocking other connections.

– Panagiotis Kanavos
Nov 13 '18 at 15:16






DBConnection is a bug. You shouldn't have a global connection. A connection should be opened right before it's used and closed as soon as possible. Locks taken while executing queries remain until a connection is closed. This means they end up blocking other connections.

– Panagiotis Kanavos
Nov 13 '18 at 15:16














Everytime you are opening the connection, where is your connection closing also you have used var conn which will neglect the static conn

– Hassaan
Nov 13 '18 at 15:18





Everytime you are opening the connection, where is your connection closing also you have used var conn which will neglect the static conn

– Hassaan
Nov 13 '18 at 15:18













If you want a generic, reusable DbConnection abstraction just use the classes already provided by ADO.NET. All ADO.NET provider classes inherit from abstract classes like DbConnection, DbCommand, DbDataReader

– Panagiotis Kanavos
Nov 13 '18 at 15:18





If you want a generic, reusable DbConnection abstraction just use the classes already provided by ADO.NET. All ADO.NET provider classes inherit from abstract classes like DbConnection, DbCommand, DbDataReader

– Panagiotis Kanavos
Nov 13 '18 at 15:18













If you worry that opening and closing a connection is costly, don't. Most ADO.NET providers implement connection pooling. When .Dispose() or .Close() is called on a connection, it's reset and put in a connection pool, ready for reuse.

– Panagiotis Kanavos
Nov 13 '18 at 15:19





If you worry that opening and closing a connection is costly, don't. Most ADO.NET providers implement connection pooling. When .Dispose() or .Close() is called on a connection, it's reset and put in a connection pool, ready for reuse.

– Panagiotis Kanavos
Nov 13 '18 at 15:19












2 Answers
2






active

oldest

votes


















3














your method defines a local var conn instead of asigning to the one you define on class level. Omit var:



 conn = new NpgsqlConnection(connstring);





share|improve this answer























  • Nice catch ... did you just washed your eyes .. :)

    – Rahul
    Nov 13 '18 at 15:16


















-1














Remove keyword 'var' from the following statement:

var conn = new NpgsqlConnection(connstring);






share|improve this answer























  • Thanks! Dont know why I put it there. Ofcourse it should be gone! Thanks a lot!

    – Daniel Strömberg
    Nov 13 '18 at 15:22











  • Please vote me if my answer makes sense!

    – Naresh
    Nov 13 '18 at 15:25










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284023%2fconnection-property-has-not-been-initialized-npgsql-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














your method defines a local var conn instead of asigning to the one you define on class level. Omit var:



 conn = new NpgsqlConnection(connstring);





share|improve this answer























  • Nice catch ... did you just washed your eyes .. :)

    – Rahul
    Nov 13 '18 at 15:16















3














your method defines a local var conn instead of asigning to the one you define on class level. Omit var:



 conn = new NpgsqlConnection(connstring);





share|improve this answer























  • Nice catch ... did you just washed your eyes .. :)

    – Rahul
    Nov 13 '18 at 15:16













3












3








3







your method defines a local var conn instead of asigning to the one you define on class level. Omit var:



 conn = new NpgsqlConnection(connstring);





share|improve this answer













your method defines a local var conn instead of asigning to the one you define on class level. Omit var:



 conn = new NpgsqlConnection(connstring);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 15:15









apomeneapomene

10.9k83457




10.9k83457












  • Nice catch ... did you just washed your eyes .. :)

    – Rahul
    Nov 13 '18 at 15:16

















  • Nice catch ... did you just washed your eyes .. :)

    – Rahul
    Nov 13 '18 at 15:16
















Nice catch ... did you just washed your eyes .. :)

– Rahul
Nov 13 '18 at 15:16





Nice catch ... did you just washed your eyes .. :)

– Rahul
Nov 13 '18 at 15:16













-1














Remove keyword 'var' from the following statement:

var conn = new NpgsqlConnection(connstring);






share|improve this answer























  • Thanks! Dont know why I put it there. Ofcourse it should be gone! Thanks a lot!

    – Daniel Strömberg
    Nov 13 '18 at 15:22











  • Please vote me if my answer makes sense!

    – Naresh
    Nov 13 '18 at 15:25















-1














Remove keyword 'var' from the following statement:

var conn = new NpgsqlConnection(connstring);






share|improve this answer























  • Thanks! Dont know why I put it there. Ofcourse it should be gone! Thanks a lot!

    – Daniel Strömberg
    Nov 13 '18 at 15:22











  • Please vote me if my answer makes sense!

    – Naresh
    Nov 13 '18 at 15:25













-1












-1








-1







Remove keyword 'var' from the following statement:

var conn = new NpgsqlConnection(connstring);






share|improve this answer













Remove keyword 'var' from the following statement:

var conn = new NpgsqlConnection(connstring);







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 15:20









NareshNaresh

614




614












  • Thanks! Dont know why I put it there. Ofcourse it should be gone! Thanks a lot!

    – Daniel Strömberg
    Nov 13 '18 at 15:22











  • Please vote me if my answer makes sense!

    – Naresh
    Nov 13 '18 at 15:25

















  • Thanks! Dont know why I put it there. Ofcourse it should be gone! Thanks a lot!

    – Daniel Strömberg
    Nov 13 '18 at 15:22











  • Please vote me if my answer makes sense!

    – Naresh
    Nov 13 '18 at 15:25
















Thanks! Dont know why I put it there. Ofcourse it should be gone! Thanks a lot!

– Daniel Strömberg
Nov 13 '18 at 15:22





Thanks! Dont know why I put it there. Ofcourse it should be gone! Thanks a lot!

– Daniel Strömberg
Nov 13 '18 at 15:22













Please vote me if my answer makes sense!

– Naresh
Nov 13 '18 at 15:25





Please vote me if my answer makes sense!

– Naresh
Nov 13 '18 at 15:25

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284023%2fconnection-property-has-not-been-initialized-npgsql-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo