Solved

Invalid connection?

Posted on 2002-07-17
2
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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