Posted By: Scott Bryen | Apr 30th, 2006 @ 11:42 AM
page 1 of 1
Comments: 23 | Views: 6574
Hi, im trying to insert a record into a database, it compiles fines. But for some reason it wont insert into the database.

Code:

public void setBookLoan()

{

// Set the cmdString to the SQL satement needed for this query.

cmdString = "INSERT dbo.book_loan (book_id, user_id, title, date_taken) VALUES (" + "'" + txtLoanBookID.Text + "'" + "'" + txtLoanUserID.Text + "'" + "'" + txtLoanTitle.Text + "'" + "'" + dateOutTimePicker.Value.Date + "'" + ")";

// Create a new connection to the database and also a new command.

SqlConnection dbConn = new SqlConnection(conn);

SqlCommand cmd = new SqlCommand(cmdString);

// Set the command properties.

cmd.CommandType = System.Data.CommandType.Text;

cmd.CommandText = cmdString;

cmd.Connection = dbConn;

// Open a connection to the database.

dbConn.Open();

// Execute query.

cmd.ExecuteNonQuery();

// Close connection to the database;

dbConn.Close();

}

Its like its missing some sort of commit statement. I dont think its my connection string because i can get data from the data base just cannot inseert any thing! Any ideas?

Thanks.

Scott B. 

W3bbo
W3bbo
The Master of Baiters
No exception was raised?
MattShepherd
MattShepherd
Ave It
isnt the correct SQL syntax "Insert INTO.."  ?

also, try using parameters for inserting data and stepping through to see whats being passed in...
MattShepherd
MattShepherd
Ave It
Add a break and run the debugger..  an incorrect SQL statement wont throw an exception
W3bbo
W3bbo
The Master of Baiters
MattShepherd wrote:
isnt the correct SQL syntax "Insert INTO.."


According to the SQL Server 2000 manual / T-SQL reference[1] the "INTO" bit is optional.

[1] See mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\tsqlref.chm::/ts_ia-iz_5cl0.htm
MattShepherd
MattShepherd
Ave It
try

VALUES ("'" + txtLoanBookID.Text + "','" + txtLoanUserID

However, id still use Parameters
W3bbo
W3bbo
The Master of Baiters
Scott Bryen wrote:

Would the lack of commas sperating the values affect it lo Tongue Out, how do u do params?

EDIT:

chnaged line to this

VALUES ('" + txtLoanBookID.Text + "','" + txtLoanUserID.Text + "', '" + txtLoanTitle.Text + "', '" + dateOutTimePicker.Value.Date + "')";

but still no luck. its as if its adding it to memmory or something and waiting to be written to the table.



"Proper" Params only work with Stored Procedures, otherwise you can use anonymous params for simple injection-proofing. More on that later.

If you're using SQL Server, have you tried using the Profiler tool to see if the database is receiving the commands and how it's executing them?

Located here: "%programfiles%\Microsoft SQL Server\80\Tools\Binn\profiler.exe"
PerfectPhase
PerfectPhase
"This is not war, this is pest control!" - Dalek to Cyberman
Is this a winforms project?  Where is the database?  What's your connection string?

I've seen people have the db in their project dir, which gets copied into the build directory and the insert is done in the copy and when you stop and restart the changes are lost.  They to spent ages looking for a reason why records do not insert Smiley
W3bbo
W3bbo
The Master of Baiters
Scott Bryen wrote:
W3bbo wrote:
If you're using SQL Server, have you tried using the Profiler tool to see if the database is receiving the commands and how it's executing them?

Located here: "%programfiles%\Microsoft SQL Server\80\Tools\Binn\profiler.exe"


I cant seem to find this tool, guess its not bundeled with express lol Tongue Out


Yep, you're right, it only comes with SQL Server 2000 Standard or above.

Which is a shame, because it's a very nifty little program.
PerfectPhase
PerfectPhase
"This is not war, this is pest control!" - Dalek to Cyberman
Scott Bryen wrote:

PerfectPhase wrote:  Is this a winforms project?  Where is the database?  What's your connection string?

I've seen people have the db in their project dir, which gets copied into the build directory and the insert is done in the copy and when you stop and restart the changes are lost.  They to spent ages looking for a reason why records do not insert Smiley


yep its winforms. I designed my datab ase using the datbase explorer in c# express. Then i aded a data source to my project it did say something about copying it to my porject and of course i pressed yes Tongue Out lol so now i have a lil data base in my solution.

this is my connection string (the one from settings.settings).

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\lms_db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True

 if i click in the little box i have this one.

Visual Studio 2005\Projects\lms\LibraryManagementSystem\LibraryManagementSystem\lms_db.mdf

so i guess its doing what u said ?


Have a look in your bin/debug directory, if you have a copy in there to, that's your problem.

If you want to preserve changes between debug runs, in the Solution Explorer, select the db file attached to the app and change the "Copy to output directory" property to "Do Not Copy".  You just have to remember to copy the version from the solution directory to the debug if you make any changes.

Other options are to change the connection string so it's hard coded to the DB in your solution dir (remember to change it back when you do a release!), our to create a hosted database in SQL and attach to that rather than doing a file attach.

Good tool for mangaging SQL Express is http://www.microsoft.com/downloads/details.aspx?familyid=C243A5AE-4BD1-4E3D-94B8-5A0F62BF7796&displaylang=en
Maurits
Maurits
AKA Matthew van Eerde
MattShepherd wrote:
isnt the correct SQL syntax "Insert INTO.."


Yup.  Standard SQL is "INSERT INTO Table VALUES ..." and "DELETE FROM Table WHERE ...".  T-SQL, as a proprietary extension, allows you to say "INSERT Table" and "DELETE Table WHERE ..." instead.
PerfectPhase
PerfectPhase
"This is not war, this is pest control!" - Dalek to Cyberman
can you zip the project up and stick it on the web so we can have a look?
W3bbo
W3bbo
The Master of Baiters
Scott Bryen wrote:
Can i send u on msn as its my coursework for computing  so i dont think i should go posting it all over the internet lol.


What a co-incidence Smiley

You are aware the AS deadline is in 2 weeks, and the A2 deadline is this Thursday?

I've posted segments of my AS coursework around before, that's fine. It's only when you post the whole thing in its entirety or any of the written part.
PerfectPhase
PerfectPhase
"This is not war, this is pest control!" - Dalek to Cyberman
All we care about is the database and the query, create a new c# project copy the db into and just the core logic arround the insert, that will be enough.
W3bbo
W3bbo
The Master of Baiters
Scott Bryen wrote:
Ok ill upload what i have to my lil geocities host and post a link. Im assuming ur at school/sixthform webbo doing a2 computing aswell? if so what did u do for ur project ?


For my AS last year, we did the "Terry's Turkeys" exercise, I didn't fare too well as I failed to meet all the criteria, so I opted to redo this year's AS courserwork: "Village Hall" as well as my A2 coursework.

I feel the Village Hall exercise was a little too hard, since it makes a requirement for bookings with multiple occurences, I think this is a little beyond most candidate's abilities (and I've been asked a few times by those in the year below me how to do it)

I actually asked Bruce Morgan, one of the IE7 developers (who used to write calendaring software) how he suggested I solve the problem. Probably overkill, but it works Smiley

For my A2 coursework, I wrote "Insecticide" as a bug-tracking solution for Adam Kinney (ironically, it has a few problems of its own), one of Channel9's developers. But Adam was too busy to finish writing the reports needed, so I got my boss over at Gamespy to write it up for me.
PerfectPhase
PerfectPhase
"This is not war, this is pest control!" - Dalek to Cyberman
Nothing wrong with that, works fine, just as I said the database in the server explorer is different from the one used at runtime (copied to the build/run directory every time the project builds)

Just make the changes here that's the best way to make it work if you want to see the changes you make in the sever explorer and between builds.

When you deploy it remember to change the connection string back to what you have at the moment

page 1 of 1
Comments: 23 | Views: 6574
Microsoft Communities