Connection property has not been initialized - Npgsql C#
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
add a comment |
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
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
add a comment |
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
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
c# npgsql
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
your method defines a local var conn
instead of asigning to the one you define on class level. Omit var
:
conn = new NpgsqlConnection(connstring);
Nice catch ... did you just washed your eyes .. :)
– Rahul
Nov 13 '18 at 15:16
add a comment |
Remove keyword 'var' from the following statement:
var conn = new NpgsqlConnection(connstring);
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
your method defines a local var conn
instead of asigning to the one you define on class level. Omit var
:
conn = new NpgsqlConnection(connstring);
Nice catch ... did you just washed your eyes .. :)
– Rahul
Nov 13 '18 at 15:16
add a comment |
your method defines a local var conn
instead of asigning to the one you define on class level. Omit var
:
conn = new NpgsqlConnection(connstring);
Nice catch ... did you just washed your eyes .. :)
– Rahul
Nov 13 '18 at 15:16
add a comment |
your method defines a local var conn
instead of asigning to the one you define on class level. Omit var
:
conn = new NpgsqlConnection(connstring);
your method defines a local var conn
instead of asigning to the one you define on class level. Omit var
:
conn = new NpgsqlConnection(connstring);
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
add a comment |
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
add a comment |
Remove keyword 'var' from the following statement:
var conn = new NpgsqlConnection(connstring);
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
add a comment |
Remove keyword 'var' from the following statement:
var conn = new NpgsqlConnection(connstring);
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
add a comment |
Remove keyword 'var' from the following statement:
var conn = new NpgsqlConnection(connstring);
Remove keyword 'var' from the following statement:
var conn = new NpgsqlConnection(connstring);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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