Solved

Invalid connection?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now