Link to home
Start Free TrialLog in
Avatar of Pandemonium1x
Pandemonium1xFlag for United States of America

asked on

C#, Database connection error

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
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of Pandemonium1x

ASKER

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]
Not sure if this helps but here is my DB tree too.
struct.png
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You deserve a rank of "Jedi" my friend. Good answer...Thanks it worked!