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
Solved

Invalid connection?

Posted on 2002-07-17
2
228 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…

808 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