|
-
December 12th, 2009, 08:18 AM
#1
Problem with ExecuteReader()
Last edited by dovobet; March 14th, 2010 at 04:24 PM.
-
December 12th, 2009, 09:07 AM
#2
Re: Problem with ExecuteReader()
 Originally Posted by dovobet
adapter = addAccount.ExecuteNonQuery();
Where 'adapter is a SqlDataAdapter...
ExecuteNonQuery returns an int..
So why are you putting it in the sqlDataAdapter..?
Try This 
string SQLstr = @"INSERT INTO accTable (AccountID, Username, Type,
Forename, Surname, Address, Town, Contact_No, Type) " +
"VALUES ( '" + id + "' , '" + username + "' , '" + forename +
"' , '" + surname + "' , '" + address +
"' , '" + town + "' , '" + contact +
"' , '" + type + "' )";
SqlCommand addAccount = new SqlCommand(SQLstr, connectionLine);
MessageBox.Show("2");
addAccount.ExecuteNonQuery();
MessageBox.Show("INSERTED RECORD");
Last edited by kristof1104; December 12th, 2009 at 09:11 AM.
Reason: Added quote
-
December 12th, 2009, 09:12 AM
#3
Re: Problem with ExecuteReader()
I'm not following your logic that if the message box doesn't pop up then the commands before the ExecuteNoQuery are successful. If the messagebox does not pop up, and you are running it through the IDE, your execution will stop on the statement where the error occurs. If you have a try/catch around some commands, the error could be anywhere within the try part.
Based on your description of the message box not showing, you have a try/catch block around this code. Could you show us more of the code, and also, please use code tags.
-
December 12th, 2009, 09:49 AM
#4
Re: Problem with ExecuteReader()
I agree with sotoasty, you should always use a try_catch block..
If the messagebox with text "2" shows up it doesn't mean the code above it is ok.
For example your input query, you're just making a string so there is no error..
but when you hit the ExecuteNonQuery() it executes that query.. and if the query has an error.. normally your program will crash.. You need the try_Catch to catch that error for you..
anyway I was looking at your insert.. and is your id defined as a number(in your db)?
if so you cant use
Code:
"VALUES ( '" + id + "' ,...
it should be
Code:
"VALUES ( " + id + " ,..
-
December 12th, 2009, 10:08 AM
#5
Re: Problem with ExecuteReader()
Last edited by dovobet; March 14th, 2010 at 04:22 PM.
-
December 12th, 2009, 10:29 AM
#6
Re: Problem with ExecuteReader()
why you do this " + "VALUES
Code:
@"INSERT INTO sahebfp9_Accounts (AccountID, Username, Type, Forename, Surname, Address, Town, Contact_No, Type) VALUES ('" + id + "' , '" + username + "' , '" + forename +"' , '" + surname + "' , '" + address + "' , '" + town + "' , '" + contact +"' , '" + type + "' )";
-
December 12th, 2009, 11:02 AM
#7
Re: Problem with ExecuteReader()
 Originally Posted by invader7
why you do this " + "VALUES
Code:
@"INSERT INTO sahebfp9_Accounts (AccountID, Username, Type, Forename, Surname, Address, Town, Contact_No, Type) VALUES ('" + id + "' , '" + username + "' , '" + forename +"' , '" + surname + "' , '" + address + "' , '" + town + "' , '" + contact +"' , '" + type + "' )";
It's just formatting.. it makes no difference
dovobet I see the problem..
you have more fields then you have values.. you have type 2times!
try
Code:
string SQLstr = @"INSERT INTO sahebfp9_Accounts (AccountID, Username,
Forename, Surname, Address, Town, Contact_No, Type) " +
"VALUES (" + id + " , '" + username + "' , '" + forename +
"' , '" + surname + "' , '" + address +
"' , '" + town + "' , '" + contact +
"' , '" + type + "' )";
as insert string
Greetz
Kristof
Last edited by kristof1104; December 12th, 2009 at 11:12 AM.
-
December 12th, 2009, 11:22 AM
#8
Re: Problem with ExecuteReader()
 Originally Posted by kristof1104
It's just formatting.. it makes no difference
dovobet I see the problem..
you have more fields then you have values.. you have type 2times!
try
Code:
string SQLstr = @"INSERT INTO sahebfp9_Accounts (AccountID, Username,
Forename, Surname, Address, Town, Contact_No, Type) " +
"VALUES (" + id + " , '" + username + "' , '" + forename +
"' , '" + surname + "' , '" + address +
"' , '" + town + "' , '" + contact +
"' , '" + type + "' )";
as insert string
Greetz
Kristof
Thanks for pointing that out Kristof However the data still will not insert into the database
I am passing the SQLConnection from another form by passing the already open connection into the constructor of the Account Creation form that holds the button. I then use
Code:
this.connection = connection;
to set the connection in the form and carry on using the open connection with my SQL statement. I thought id mention it incase that may be the problem...
-
December 12th, 2009, 11:57 AM
#9
Re: Problem with ExecuteReader()
Strange..,
could you upload your project and db so I can take a look at it for you?
-
December 12th, 2009, 11:58 AM
#10
[RESOLVED] Problem with ExecuteReader()
I have now resolved this problem. I had to open and close the connection in the method before and after the executenonquery() was done. Thanks for the help guys
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|