deleted
Printable View
deleted
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");
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.
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 useit should beCode:"VALUES ( '" + id + "' ,...
Code:"VALUES ( " + id + " ,..
deleted
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
as insert stringCode:string SQLstr = @"INSERT INTO sahebfp9_Accounts (AccountID, Username,
Forename, Surname, Address, Town, Contact_No, Type) " +
"VALUES (" + id + " , '" + username + "' , '" + forename +
"' , '" + surname + "' , '" + address +
"' , '" + town + "' , '" + contact +
"' , '" + type + "' )";
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 useto 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...Code:this.connection = connection;
Strange..,
could you upload your project and db so I can take a look at it for you?
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:)