Results 1 to 5 of 5

Thread: SQL statement problem in C#

  1. #1
    Join Date
    Jun 2009
    Posts
    53

    SQL statement problem in C#

    Code:
    void add(int accnum,string username, string password, string m, string fn, string ln, string college) {
            OleDbConnectionStringBuilder ocsb;
            OleDbConnection conn;
            OleDbCommand myCmd;
            OleDbDataReader reader;
            OleDbParameter p, q;
            conn.Open();
    string iSQL = "INSERT INTO AccountData(username, passwrd, email, firstName, lastName, college) VALUES(username, password, m, fn, ln, college)";
                myCmd = new OleDbCommand(iSQL, conn);
                myCmd.Parameters.Add("username", OleDbType.VarChar).Value = username;
                myCmd.Parameters.Add("password", OleDbType.VarChar).Value = password;
                myCmd.Parameters.Add("m", OleDbType.VarChar).Value = m;
                myCmd.Parameters.Add("fn", OleDbType.VarChar).Value = fn;
                myCmd.Parameters.Add("ln", OleDbType.VarChar).Value = ln;
                myCmd.Parameters.Add("college", OleDbType.VarChar).Value = college;
                myCmd.ExecuteNonQuery();
                conn.Close();
            }
    The above code is giving me the syntax error for insert statement. What is wrong in my code? I am not figuring out the actual cause of the error message.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: SQL statement problem in C#

    I didn't find any mistake in your code. Everything is perfect. The only possibility according to me for the error message is if any of your field is left blank. In case, for example, you have not entered the email address of the username then that may result in this kind of error message. I would recommend you to use "try..catch" statements to handle any unconditional situations that might occur.

  3. #3
    Join Date
    Nov 2005
    Posts
    1,323

    Re: SQL statement problem in C#

    I suppose your mistake was in using the same name as the field name. You have used the same names as username, password, email, firstName, lastName, college for both defining the field name as well as for the variable. In such situations, C# forces the user to use "@" symbol to differentiate between the two.

  4. #4
    Join Date
    Nov 2008
    Posts
    1,221

    Re: SQL statement problem in C#

    I agree to all the points discussed so far but I would also add one more point that this is not the actual reason for getting the error message at "INSERT" statement.

    Code:
    string iSQL = "INSERT INTO AccountData(username, passwrd, email, firstName, lastName, college) VALUES(username, password, m, fn, ln, college)";
    You are getting that error message because you have misspelled the word "password" as "passwrd". So the compiler is assuming this as new field and thus the message.

  5. #5
    SNK111 Guest

    Re: SQL statement problem in C#

    Check this link HOW TO: SQL & C# for details on how to connect to SQL server database from C#.NET database applications as well as Java database applications. It also describes how to pass embedded SQL queries, calling stored procedures, pass parameter etc.

Similar Threads

  1. Problem of WHERE clause in SQL statement
    By Macario in forum Software Development
    Replies: 4
    Last Post: 15-10-2010, 08:02 AM
  2. Problem with Prepared statement's 'setDate' method
    By Luis-Fernando in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 08:19 PM
  3. Problem with if statement in C++
    By Slender in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 09:57 PM
  4. Problem related to null value in sql statement
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 28-01-2010, 07:36 PM
  5. Replies: 4
    Last Post: 25-02-2009, 08:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,636,626.71204 seconds with 17 queries