Solved

Invalid connection?

Posted on 2002-07-17
2
230 Views
Last Modified: 2008-02-01
This is the code I use:
-----------------------------
SqlConnection myConnection = new SqlConnection("Data Source=" + Server.MapPath("nyheter.mdb") + ";");
string myInsertQuery = "INSERT INTO tblNyheter (nyhetsRubrik, nyhetsText, nyhetsUser) Values('" + strRubrik + "', '" + strText + "', '" + strUser + "')";
SqlCommand myCommand = new SqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
-----------------------------

I get the error message "Invalid connection" when on the line "myConnection.Open();".

Anyone who can help me out?

I'd appreciate a quick answer (more points to earn).
0
Comment
Question by:SilvioF
2 Comments
 
LVL 4

Accepted Solution

by:
kpkp earned 75 total points
ID: 7159188
There are a few problems with your code.

For MS Access database updates you need to use the System.Data.OleDb namespace, not System.Data.SqlClient.  So you would use OleDbConnection not SqlConnection.  And OleDbCommand not SqlCommand.

Also you connection string doesn't have a provider element, this should be something like Provider=Microsoft.Jet.OLEDB.4.0;

Finally, it's better practice to use the OleDBCommand.Parameters collection to include variable items in your sql string.. do something like

string myInsertQuery = "INSERT INTO tblNyheter (nyhetsRubrik, nyhetsText, nyhetsUser) Values(?, ?, ?)";

System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(myInsertQuery);
myCommand.Parameters.Add(new System.Data.OleDb.OleDbParameter("nyhetsRubrik", strRubrik));
myCommand.Parameters.Add(new System.Data.OleDb.OleDbParameter("nyhetsText", strText));
myCommand.Parameters.Add(new System.Data.OleDb.OleDbParameter("nyhetsUser", strUser)):
0
 

Author Comment

by:SilvioF
ID: 7159196
Thanks. It worked like a charm.  =)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question