CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2009
    Posts
    90

    Problem with ExecuteReader()

    deleted
    Last edited by dovobet; March 14th, 2010 at 04:24 PM.

  2. #2
    Join Date
    Sep 2006
    Posts
    31

    Re: Problem with ExecuteReader()

    Quote Originally Posted by dovobet View Post
    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

  3. #3
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    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.

  4. #4
    Join Date
    Sep 2006
    Posts
    31

    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 + " ,..

  5. #5
    Join Date
    Dec 2009
    Posts
    90

    Re: Problem with ExecuteReader()

    deleted
    Last edited by dovobet; March 14th, 2010 at 04:22 PM.

  6. #6
    Join Date
    Jun 2009
    Posts
    144

    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 + "' )";

  7. #7
    Join Date
    Sep 2006
    Posts
    31

    Re: Problem with ExecuteReader()

    Quote Originally Posted by invader7 View Post
    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.

  8. #8
    Join Date
    Dec 2009
    Posts
    90

    Unhappy Re: Problem with ExecuteReader()

    Quote Originally Posted by kristof1104 View Post
    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...

  9. #9
    Join Date
    Sep 2006
    Posts
    31

    Re: Problem with ExecuteReader()

    Strange..,
    could you upload your project and db so I can take a look at it for you?

  10. #10
    Join Date
    Dec 2009
    Posts
    90

    [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
  •  





Click Here to Expand Forum to Full Width

Featured