problems with insert in Csharp


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: problems with insert in Csharp

  1. #1
    Join Date
    Mar 2006
    Posts
    11

    problems with insert in Csharp

    I made this small app in c# that should add a row in a player table. i can select from the table and traverse rows in the table, but when i insert something nothing happens. I use executenonquery() and it returns 1, which sshould indicate 1 row was inserted, but nothing is inserted !!!!

    Could someone help me with this, my code is :


    Code:
      int Tycoon.ServerInterface.addNewPlayer(string forname, string lastname, string email, DateTime birthdate, string country, string username, string pword)
            {
    
    
                String select = "select * from Player where username='" + username+"'";
                SqlCommand cmd = new SqlCommand(select, conn);
                conn.Open();
                SqlDataReader rows = cmd.ExecuteReader();
                
                
                if (rows.HasRows)
                {
                    return -1;
                }
                conn.Close();
                String insert = "insert into player (forname,lastname,country,email,birthdate,username,password,lastlogin) values (@forname,@lastname,@country,@email,@birthdate,@username,@password,@lastlogin)";
                
    
                cmd = new SqlCommand(insert, conn);
                cmd.Parameters.Add("@forname", System.Data.SqlDbType.VarChar).Value = forname;
                cmd.Parameters.Add("@lastname", SqlDbType.VarChar).Value = lastname;
                cmd.Parameters.Add("@country", SqlDbType.VarChar).Value = country;
                cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = email;
                cmd.Parameters.Add("@birthdate", SqlDbType.DateTime).Value = birthdate;
                cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
                cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = pword;
                cmd.Parameters.Add("@lastlogin", SqlDbType.DateTime).Value = DateTime.Now;
                
                conn.Open();
                int numRows = cmd.ExecuteNonQuery();
                conn.Close();
                if (numRows == 1)
                {
    
                    return 0;
                }
                else
                {
    
                    return -2;
                }
    
    
    
    
            }

  2. #2
    Join Date
    Dec 2004
    Posts
    163
    sundruid,

    Could it be that you are actually updating a copy of your database? See:

    http://msdn2.microsoft.com/en-us/library/ms246989.aspx

    Kerry Moorman

  3. #3
    Join Date
    Mar 2006
    Posts
    11
    Yes, thx Kerry, that was exactly the case. My connectionstring pointed at the build copy of the database. Changed the string and it works fine now.

    thx again,

    Sundruid

Similar Threads

  1. how to clone database using mysql
    By zarien in forum Database
    Replies: 1
    Last Post: 05-17-2006, 03:27 AM
  2. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 06:24 PM
  3. SQL insert, problems
    By Fernando in forum ASP.NET
    Replies: 14
    Last Post: 01-19-2001, 03:33 AM
  4. Replies: 2
    Last Post: 04-13-2000, 01:53 PM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center