-2

In Visual Studio Form, Im creating a c# application. Im an trying to add data entered into a register form and save it into the MDF Database file i cretased with tables. But i cant connect to the database, Any Help?

My Register Button Code when its clicked

private void CreateAccBtn_Click(object sender, EventArgs e)
{
    string ConnectToDatabase = ("Data Source=  (LocalDB)\v11.0;AttachDbFilename=E:\\Work\\Information Systems      Development\\Game\\My Brain Cognitive Game\\My Brain Cognitive Game\\Brain Game Database.mdf;Integrated Security=True;Connect Timeout=10");
    SqlConnection ConnectDatabase = new SqlConnection(ConnectToDatabase);
    SqlCommand CMDDatabase = new SqlCommand("Insert Into Brain Game Database.User Table (User Name, Date Of Birth, Gender, Date Account Created, Condition, Email, Password) values('" + this.NewUsernameTxtBox.Text + "','" + this.DOBDateTimePicker3.Text + "','" + this.SexListBox1.Text + "','" + this.CurrentDatePicker1.Text + "','" + this.NewConditionTxtBox.Text + "','" + this.NewEmailTxtBox.Text + "','" + this.NewPasswordTxtBox.Text + "');",ConnectDatabase);
    SqlDataReader MyReader;
    try
    {
         ConnectDatabase.Open();
         MyReader = CMDDatabase.ExecuteReader();
         MessageBox.Show("Account Created");
         while (MyReader.Read())
         {

         }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}  
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Rajan
  • 21
  • 5
  • 1
    `But i cant connect to the database` what does this mean? What error message do you get? What research have you done prior asking here? Pretty much every error message you could get IS already answered and explained numerous times.. So what makes your case specific that you decided to create a new question? – walther Jan 20 '15 at 17:07
  • [C# ConnectionStrings For Databases](http://www.connectionstrings.com) also can you place the file in a shorter file path just curious with those spaces `AttachDbFilename=E:\\Work\\Information Systems Development\\Game\\My Brain Cognitive Game\\My Brain Cognitive Game\\Brain Game Database.mdf;` I would suggest prepending the `@` literal – MethodMan Jan 20 '15 at 17:09
  • it just says " A network-related or instance-specific error occurred while establishing a connection to a SQL Server. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) – Rajan Jan 20 '15 at 17:30

1 Answers1

0

There are a few things to help you get connected. I just helped several students had their connections done this week.

Please double check your connection string is correct (pointing to the right location - path of your .MDF file)

  1. Make sure the connection string is correct.
  2. Try not to have duplicate database in your project.
  3. Make sure the path to your database in all settings are correct (having duplicate database may confuse you here)
  4. When adding new connection, there is a Test Connection button. Click it and ensure connection is working before you try to execute query from the codes.
  5. Once everything is done, Use the Select * FROM tblName query statement to test first instead of using an insert statement which is more prone to typo error.

You can try the following (instead of using a SqlCommand object):

string queryStr = "YOUR SQL QUERY HERE";
SqlCommand cmd = new SqlCommand(queryStr, new SqlConnection(connectionString));
cmd.Connection = sqlConnection;
sqlConnection.Open();
cmd.ExecuteNonQuery();
sqlConnection.Close();
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • I have one MDF file in the original folder, and I have the same database in the bin/Debug folder. The test connection does succeed, I then click advanced settings and copied the Data Source link at the bottom, and pasted that into the ConnectToDatabse string. – Rajan Jan 20 '15 at 17:36
  • @Rajan I see long spaces in your connection string, so maybe you really want to ensure the connection string in your codes are correct. – user3437460 Jan 20 '15 at 17:38
  • the spaces only appeared when i submitted the question as code you need to indent when asking the question. – Rajan Jan 20 '15 at 17:48
  • @Rajan Anyway, I added some codes in my solution, you can trying using those codes for connection and executing the query string instead of your original codes which uses `SqlCommand`. – user3437460 Jan 20 '15 at 17:50
  • look at the connection strings link that was provided in the initial comments above.. – MethodMan Jan 20 '15 at 18:07