1

here is my code

 private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string connectionString = @"Data Source=|DataDirectory|\Database_POS.sdf";
            using (SqlConnection connection = new SqlConnection(connectionString))
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText= "INSERT INTO Customer (Email) Values (@email);";
                command.Parameters.AddWithValue("@email",textBox_Email.Text);

                connection.Open();
                command.ExecuteNonQuery(); 
            }
        }

        catch (SqlException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }  

here is my design

enter image description here

    I am only trying to save 'email' here. 

Here is my sql table, problem is I can not even save a single field into table .

enter image description here

Database Design enter image description here

Now error is this

enter image description here

10
  • 2
    what error your getting here.. Commented Nov 21, 2013 at 5:57
  • Is connectionString correct , have you trace using break point ? Commented Nov 21, 2013 at 6:01
  • are you getting a value for textbox_Email.text when you pass it to command.parameters.addwithvalue() ? How did you get the other values into the database Commented Nov 21, 2013 at 6:06
  • here is my connection string <add name="DatabaseConnectionString" connectionString="Data Source=|DataDirectory|\Database_POS.sdf" providerName="Microsoft.SqlServerCe.Client.3.5" /> Commented Nov 21, 2013 at 6:06
  • 1
    No problem in your code. But there are a lot of problems in your design! Does your table have a primary key? The Id field doesn't look like a primary key because it is nullable. Without a primary key how are you going to identify your records? Commented Nov 21, 2013 at 6:07

2 Answers 2

1

Some things to check:

  1. Do you event get an error? If yes, what is the error? Be aware that you are writing the error message to the Console window, but your application is a Windows Application. Does your application have a Console window? If no, check the Output window in Visual Studio. Or simpler yet, use MessageBox.Show to show the error.

  2. If program runs without any error, check the return value of command.ExecuteNonQuery(). It tells you how many records affected. If it is more that zero, the value is really being stored in the database.

  3. Make sure that you are seeing the contents of the database file that you are inserting to in your program! You may have opened Database_POS.sdf from your project directory, but the program inserts the data into another Database_POS.sdf in your "bin\Debug" directory.

-- More Help --

Change your method to:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        string connectionString = @"Data Source=|DataDirectory|\Database_POS.sdf";
        using (SqlConnection connection = new SqlConnection(connectionString))
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandText= "INSERT INTO Customer (Email) Values (@email);";
            command.Parameters.AddWithValue("@email",textBox_Email.Text);

            connection.Open();
            var rowsAffected = command.ExecuteNonQuery();
            if(rowsAffected > 0)
                MessageBox.Show("Data inserted successfully");
            else
                MessageBox.Show("Nothing inserted into the database!");
        }
    }

    catch (SqlException ex)
    {
        Console.WriteLine(ex.Message);
        MessageBox.Show(ex.ToString()); // for debugging purpose
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

thanks everyone for answers

Cannot connect to local database in C#

I got my answer form above link.

I am using Compact Database and Trying to connect like SQLServer.

Now got it working.

here is the answer text ,that solve my question

When using a .sdf file (SQL Server Compact Edition), you need to use SqlCeConnection and not SqlConnection (that works against a full SQL Server):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.