I am getting an error whenever I am saving the data to the SQL Server database using C# [duplicate]
This question already has an answer here:
What is “Connect Timeout” in sql server connection string?
7 answers
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: UsersFrancissourcereposPersonal Expenses SystemNumData.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.
c# sql
marked as duplicate by Ken White, Alexei Levenkov
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 2:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
This question already has an answer here:
What is “Connect Timeout” in sql server connection string?
7 answers
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: UsersFrancissourcereposPersonal Expenses SystemNumData.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.
c# sql
marked as duplicate by Ken White, Alexei Levenkov
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 2:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
If you Google foran 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 3 at 2: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 3 at 2:12
I guess the error was I reported earlier was different error for what I attached here.
– Francis Naval
Jan 3 at 2: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 3 at 2:37
Any luck updating your question with the suggestions you've already tried?
– mjwills
Jan 3 at 4:24
|
show 1 more comment
This question already has an answer here:
What is “Connect Timeout” in sql server connection string?
7 answers
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: UsersFrancissourcereposPersonal Expenses SystemNumData.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.
c# sql
This question already has an answer here:
What is “Connect Timeout” in sql server connection string?
7 answers
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: UsersFrancissourcereposPersonal Expenses SystemNumData.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.
This question already has an answer here:
What is “Connect Timeout” in sql server connection string?
7 answers
c# sql
c# sql
edited Jan 3 at 6:35
marc_s
582k13011221269
582k13011221269
asked Jan 3 at 1:53
Francis NavalFrancis Naval
33
33
marked as duplicate by Ken White, Alexei Levenkov
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 2:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ken White, Alexei Levenkov
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 2:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
If you Google foran 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 3 at 2: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 3 at 2:12
I guess the error was I reported earlier was different error for what I attached here.
– Francis Naval
Jan 3 at 2: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 3 at 2:37
Any luck updating your question with the suggestions you've already tried?
– mjwills
Jan 3 at 4:24
|
show 1 more comment
1
If you Google foran 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 3 at 2: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 3 at 2:12
I guess the error was I reported earlier was different error for what I attached here.
– Francis Naval
Jan 3 at 2: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 3 at 2:37
Any luck updating your question with the suggestions you've already tried?
– mjwills
Jan 3 at 4:24
1
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 3 at 2:05
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 3 at 2:05
1
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 3 at 2:12
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 3 at 2:12
I guess the error was I reported earlier was different error for what I attached here.
– Francis Naval
Jan 3 at 2:24
I guess the error was I reported earlier was different error for what I attached here.
– Francis Naval
Jan 3 at 2: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 3 at 2:37
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 3 at 2:37
Any luck updating your question with the suggestions you've already tried?
– mjwills
Jan 3 at 4:24
Any luck updating your question with the suggestions you've already tried?
– mjwills
Jan 3 at 4:24
|
show 1 more comment
1 Answer
1
active
oldest
votes
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:)
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
– Michael Randall
Jan 3 at 2:09
Thanks for the advise! I'll avoid answer while I cannot mark a question as duplicated.
– mourato
Jan 3 at 2:29
I am pretty sure this is the correct path. I am using local sql database via visual studio 2017.
– Francis Naval
Jan 3 at 2: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 (stackoverflow.com/questions/8926512/…).
– mourato
Jan 3 at 2:39
I copied the connection string of the database itself but still getting the error
– Francis Naval
Jan 3 at 2:58
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:)
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
– Michael Randall
Jan 3 at 2:09
Thanks for the advise! I'll avoid answer while I cannot mark a question as duplicated.
– mourato
Jan 3 at 2:29
I am pretty sure this is the correct path. I am using local sql database via visual studio 2017.
– Francis Naval
Jan 3 at 2: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 (stackoverflow.com/questions/8926512/…).
– mourato
Jan 3 at 2:39
I copied the connection string of the database itself but still getting the error
– Francis Naval
Jan 3 at 2:58
|
show 2 more comments
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:)
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
– Michael Randall
Jan 3 at 2:09
Thanks for the advise! I'll avoid answer while I cannot mark a question as duplicated.
– mourato
Jan 3 at 2:29
I am pretty sure this is the correct path. I am using local sql database via visual studio 2017.
– Francis Naval
Jan 3 at 2: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 (stackoverflow.com/questions/8926512/…).
– mourato
Jan 3 at 2:39
I copied the connection string of the database itself but still getting the error
– Francis Naval
Jan 3 at 2:58
|
show 2 more comments
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:)
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:)
edited Jan 3 at 2:28
answered Jan 3 at 2:06
mouratomourato
13
13
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
– Michael Randall
Jan 3 at 2:09
Thanks for the advise! I'll avoid answer while I cannot mark a question as duplicated.
– mourato
Jan 3 at 2:29
I am pretty sure this is the correct path. I am using local sql database via visual studio 2017.
– Francis Naval
Jan 3 at 2: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 (stackoverflow.com/questions/8926512/…).
– mourato
Jan 3 at 2:39
I copied the connection string of the database itself but still getting the error
– Francis Naval
Jan 3 at 2:58
|
show 2 more comments
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
– Michael Randall
Jan 3 at 2:09
Thanks for the advise! I'll avoid answer while I cannot mark a question as duplicated.
– mourato
Jan 3 at 2:29
I am pretty sure this is the correct path. I am using local sql database via visual studio 2017.
– Francis Naval
Jan 3 at 2: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 (stackoverflow.com/questions/8926512/…).
– mourato
Jan 3 at 2:39
I copied the connection string of the database itself but still getting the error
– Francis Naval
Jan 3 at 2:58
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
– Michael Randall
Jan 3 at 2:09
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
– Michael Randall
Jan 3 at 2:09
Thanks for the advise! I'll avoid answer while I cannot mark a question as duplicated.
– mourato
Jan 3 at 2:29
Thanks for the advise! I'll avoid answer while I cannot mark a question as duplicated.
– mourato
Jan 3 at 2:29
I am pretty sure this is the correct path. I am using local sql database via visual studio 2017.
– Francis Naval
Jan 3 at 2:32
I am pretty sure this is the correct path. I am using local sql database via visual studio 2017.
– Francis Naval
Jan 3 at 2: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 (stackoverflow.com/questions/8926512/…).
– mourato
Jan 3 at 2:39
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 (stackoverflow.com/questions/8926512/…).
– mourato
Jan 3 at 2:39
I copied the connection string of the database itself but still getting the error
– Francis Naval
Jan 3 at 2:58
I copied the connection string of the database itself but still getting the error
– Francis Naval
Jan 3 at 2:58
|
show 2 more comments
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 3 at 2: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 3 at 2:12
I guess the error was I reported earlier was different error for what I attached here.
– Francis Naval
Jan 3 at 2: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 3 at 2:37
Any luck updating your question with the suggestions you've already tried?
– mjwills
Jan 3 at 4:24