?
Solved

C#, Database connection error

Posted on 2011-03-25
5
Medium Priority
?
626 Views
Last Modified: 2013-12-17
Good Morning all,

I am writing a test program to connect a C# WinForm to a test database .SDF file. I have managed to populate the database and make it show up in a datagridview already but now I am trying to add data during runtime. At first I am hard coding the values to add but when I do it I get an error about the User ID. I am not sure what to code for it because when I created the database no user ID was defined only the password. Here is my code

 
SqlCeConnection sqlConnection1 = new SqlCeConnection();
            sqlConnection1.ConnectionString = "Data Source = C:\\Users\\smunsell\\Documents\\TestDB.sdf; User ID = ???; Password = Password";

            System.Data.SqlServerCe.SqlCeCommand cmd = new System.Data.SqlServerCe.SqlCeCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "INSERT [UserData] (ID, User, Status) VALUES ('12', 'Tim', 'Inactive')";
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            sqlConnection1.Close();

Open in new window


Any ideas how to fix it or what I should put in my uer id? Screenshot of program attached. Error code as well.

screen.png
error.png
0
Comment
Question by:Pandemonium1x
[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
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35215678
I don't think you need to pass a user id for a CE connection string. Just remove it so your string becomes:
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\smunsell\\Documents\\TestDB.sdf; Password = Password";

Open in new window

0
 
LVL 4

Author Comment

by:Pandemonium1x
ID: 35215925
I have thought of that too but if I remove it I get


There was an error parsing the query. [Tolen line number =1, Token line offset = 20, Token in error = User]
0
 
LVL 4

Author Comment

by:Pandemonium1x
ID: 35215958
Not sure if this helps but here is my DB tree too.
struct.png
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 35215960
I think it complaining because User is a reserved word, and it is referring to your query rather than the connection string.

Try changing:
cmd.CommandText = "INSERT [UserData] (ID, User, Status) VALUES ('12', 'Tim', 'Inactive')";

Open in new window

To:
cmd.CommandText = "INSERT [UserData] (ID, \"User\", Status) VALUES ('12', 'Tim', 'Inactive')";

Open in new window

0
 
LVL 4

Author Closing Comment

by:Pandemonium1x
ID: 35216024
You deserve a rank of "Jedi" my friend. Good answer...Thanks it worked!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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