Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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 :

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;
}

}
[2264 byte] By [sundruid] at [2007-11-11 10:06:05]
# 1 Re: problems with insert in Csharp
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
kmoorman at 2007-11-11 20:48:32 >
# 2 Re: problems with insert in Csharp
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
sundruid at 2007-11-11 20:49:33 >