Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Inserting data to .mdf file

Hey,

I've been looking at dozens of stackoverflow answers regarding this and none seem to really be relevant to me. I need to do this assignment for class where we use a .mdf file for our database (not sql server external db) and have certain management on this mdf.

When I go to insert into the .mdf, nothing is saving. I read that it's because it saves into the bin/debug, but I don't even have that folder so I can't check. I' not getting any errors and when I had a try..catch..finally it hit the finally with no problems and even when I removed that and just had try catch it was fine, no exception caught.

I can read from the database fine I just can't insert into it. It's really annoying because our professor always used front end sqldataadaptors in class and i'm trying to do it on the back end because I feel like that's better for me in the long run, but I'll do it his way if I absolutely have to.

Anyone know how to solve this?
01000101
I thought you had to connect to that .mdf file before you could modify it.
(Dec 4, 2016 01:41 PM)Geekie Wrote:  I thought you had to connect to that .mdf file before you could modify it.

I'm connected in server explorer, and this is what it looks like in my code behind:

Code
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WebRanking"].ToString());
        SqlCommand cmd = new SqlCommand();

That connection string works fine on my view page. Like I said, I can connect and read fine, I just can't insert.
01000101
does it throw an exception? what does it say in the console?
(Dec 4, 2016 01:51 PM)mrkoolaid Wrote:  does it throw an exception? what does it say in the console?

No exception, console is empty. Check this stackoverflow post, it's pretty much exactly the same
http://stackoverflow.com/questions/32302...oesnt-work
01000101
are you using the right connection string?

i have not downloaded the code, if you throw it up on pastebin, that would help
(Dec 4, 2016 01:58 PM)mrkoolaid Wrote:  are you using the right connection string?

i have not downloaded the code, if you throw it up on pastebin, that would help

The connection string is fine. There are no errors. I've said this, and I don't mean to be rude, but I am able to read the data fine. I have a try { .. } catch(SqlException ex) { .. } and it executes the try perfectly fine. The problem is with the mdf file, I just don't know what it is.
01000101
do you catch only the sqlexception? have you tried the generic exception (Exception)?
How about GRANT permissions?
If you can perform SELETCS but fails with INSERTS (and probably UPDATES and DELETES) you have a permissions problem. Check GRANTS with MSSQL Management Studio (download latest version from MS or MSDN)
(Dec 4, 2016 02:17 PM)mrkoolaid Wrote:  do you catch only the sqlexception? have you tried the generic exception (Exception)?
I'll give it a shot but it'd just error and give me uncaughtexception I think if there was another exception.
(Dec 4, 2016 02:23 PM)Zilog80 Wrote:  How about GRANT permissions?
If you can perform SELETCS but fails with INSERTS (and probably UPDATES and DELETES) you have a permissions problem. Check GRANTS with MSSQL Management Studio (download latest version from MS or MSDN)
There shouldn't be a permission problem because I'm able to insert from the server explorer itself.
01000101
Just to rule things out , have you tryed to delete your database from server explorer , double click your database from solution explorer and check if its not inserted ? I remember having that issue when starting with databases. Also make sure that your mdf file's property is set to copy always.

"I read that it's because it saves into the bin/debug, but I don't even have that folder so I can't check"

Why don't you have that folder ? 😧

🤝
(Dec 4, 2016 02:54 PM)GuRuHaX Wrote:  Just to rule things out , have you tryed to delete your database from server explorer , double click your database from solution explorer and check if its not inserted ? I remember having that issue when starting with databases. Also make sure that your mdf file's property is set to copy always.

"I read that it's because it saves into the bin/debug, but I don't even have that folder so I can't check"

Why don't you have that folder ? 😧

🤝

I don't know why I don't have it, and what you said is basically what was wrong. I don't know why it's like that. I closed VS and reopened the project and all the data had been there the whole time.
01000101


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by datguy.devSep 16, 2016 11:34 AM 2 77 Sep 16, 2016 11:34 AM
Last Post: datguy.dev
[.NET] by antibiotic1Jul 29, 2016 06:07 AM 1 119 Jul 29, 2016 06:07 AM
Last Post: Zilog80
[.NET] by Netflix and WeedApr 17, 2016 03:04 PM 4 144 Apr 17, 2016 03:04 PM
Last Post: Netflix and Weed