-4

I am working with my personal project which is a personal expenses system. I am trying to save data to SQL Server, but I am always getting an error:

"0x80131904" an attempt to attach an auto name database. A database with the same name exists. or specified file cannot be opened or it is located on UNC share"

To give you the complete error I attached here.

Thanks.

try
{ 
    SqlConnection conn =  new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\Francis\source\repos\Personal Expenses System\NumData.mdf;Integrated Security=True;Connect Timeout=30");

    conn.Open();

    string insert_query = "INSERT INTO [Table111] (House Rent, NB Power, Car Insurance, Life Insurance, Gasoline, Grocery, Rogers Internet, kodoo Mobile, Laundry, Tithes, Padala, Extra, Total, Gross Income, Net Income) VALUES (@House Rent, @NB Power, @Car Insurance, @Life Insurance, @Gasoline, @Grocery, @Rogers Internet, @kodoo Mobile, @Laundry, @Tithes, @Padala, @Extra, @Total, @Gross Income, @Net Income)";

    SqlCommand cmd = new SqlCommand(insert_query, conn);

    cmd.Parameters.AddWithValue("@House Rent", textbox1.Text);
    cmd.Parameters.AddWithValue("@NB Power", textBox2.Text);
    cmd.Parameters.AddWithValue("@Car Insurance", textBox3.Text);
    cmd.Parameters.AddWithValue("@Life Insurance", textBox4.Text);
    cmd.Parameters.AddWithValue("@Gasoline", textBox5.Text);
    cmd.Parameters.AddWithValue("@Grocery", textBox6.Text);
    cmd.Parameters.AddWithValue("@Rogers Internet", textBox7.Text);
    cmd.Parameters.AddWithValue("@kodoo Mobile", textBox8.Text);
    cmd.Parameters.AddWithValue("@Laundry", textBox9.Text);
    cmd.Parameters.AddWithValue("@Tithes", textBox10.Text);
    cmd.Parameters.AddWithValue("@Padala", textBox11.Text);
    cmd.Parameters.AddWithValue("@Extra", textBox12.Text);
    cmd.Parameters.AddWithValue("@Total", textBox13.Text);
    cmd.Parameters.AddWithValue("@Gross Income", textBox14.Text);
    cmd.Parameters.AddWithValue("@Net Income", textBox15.Text);

    cmd.ExecuteNonQuery();

    MessageBox.Show("Record SAVE");
    conn.Close();
}
catch (Exception ex)
{
    MessageBox.Show("EROR:" + ex.ToString());
}

I attached the screen shot of the error. It was lengthy one.

ERROR SQL

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    If you Google for `an attempt to attach the auto named database failed` there are a number of suggestions. Can you update your question to make clear which of those suggestions you have already tried? – mjwills Jan 03 '19 at 02:05
  • 1
    Also: make sure to post error messages as *text* not as an image. It is impossible for future visitors to find questions that solve they problems if errors/code is shown as image. – Alexei Levenkov Jan 03 '19 at 02:12
  • I guess the error was I reported earlier was different error for what I attached here. – Francis Naval Jan 03 '19 at 02:24
  • I attached the error as an image because it was lengthy but I tried to put as a text the beginning error. – Francis Naval Jan 03 '19 at 02:37
  • Any luck updating your question with the suggestions you've already tried? – mjwills Jan 03 '19 at 04:24
  • **Side note:** `decimal(18,0)` as datatype seems odd - this means 18 digits, but **none** after the decimal point - so you're basically giving up on all cent values and can store only dollar amounts - is that what you wanted?? Otherwise change it to e.g. `decimal(18,2)` which means 18 digits in all, 2 of which after the decimal point, so you can store amounts with 2 digits after the decimal point – marc_s Jan 03 '19 at 06:37

1 Answers1

0

The problem is probably happening because you're not defining a correct path for your database file NumData.mdf. Using your error message "an attempt to attach an auto named database for file" I found this answer here:)

mourato
  • 1
  • 3
  • This is not an answer, you can make it an answer by using your own words and experience to describe the problem and solution and it is some how different to the duplicate you referenced – TheGeneral Jan 03 '19 at 02:09
  • Thanks for the advise! I'll avoid answer while I cannot mark a question as duplicated. – mourato Jan 03 '19 at 02:29
  • I am pretty sure this is the correct path. I am using local sql database via visual studio 2017. – Francis Naval Jan 03 '19 at 02:32
  • Not sure if I'm helpful at this time, but, I suggest you to try another search terms to find this answer here, for example "sql connection string mdf". I found this one based on your comment (https://stackoverflow.com/questions/8926512/how-do-i-connect-to-an-mdf-database-file). – mourato Jan 03 '19 at 02:39
  • I copied the connection string of the database itself but still getting the error – Francis Naval Jan 03 '19 at 02:58
  • If you're using locally on your computer you could try to attach the .mdf file to SQL Express and connect using a different connectionString. – mourato Jan 03 '19 at 10:07
  • I am trying to connect on the local sql which is built-in to visual studio 2017. I am not sure if I understand clearly your suggestion. would you be able to elaborate please. I am new to programming. Thanks – Francis Naval Jan 03 '19 at 16:46