Solved

Invalid connection?

Posted on 2002-07-17
2
227 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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