hi,
im making a page where users click on the "REGISTER" button
to register into the program. the program gives the user a username by
taking the first 3 characters of the first name and first 3 characters
of the last name. if the username already exists i just add an
incrementing number to it, for example username1, username2..etc.
i
dont get any error when i run this code but its not inserting it into
the database either and i cant figure out what the problem is.
Any help is greatly appreciated!
thanks from now!
[code]
private void button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\AltarianChessDB.mdf;Integrated Security=True;User Instance=True";
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT Username from Players", con);
DataTable dt1 = new DataTable();
da.Fill(dt1);
string uname = firstnametxt.Text.Substring(0, 3) + secondnametxt.Text.Substring(0, 3);
bool found = false;
int j=0;
for (int i = 0; i < dt1.Rows.Count; i++)
{
if (dt1.Rows[i][0].ToString().Substring(0,6) == uname)
{
found = true;
j=i;
}
}
if (found == true)
{
string numstring = dt1.Rows[j][0].ToString().Substring(6, dt1.Rows[j][0].ToString().Length);
int num = Convert.ToInt16(numstring) + 1;
numstring = num.ToString();
uname = uname + numstring;
}
con.Close();
con.Open();
int creditvalue = 30;
System.Data.SqlClient.SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText=("insert into Players (Username,FirstName,SecondName,PlanetOfOrigin,Password,Credit) values ('"+uname+"','"+firstnametxt.Text.Trim()+"','"+secondnametxt.Text.Trim()+"','"+planetoforigintxt.Text.Trim()+"','"+passwordtxt.Text.Trim()+"','"+creditvalue+"')");
cmd.ExecuteNonQuery();
MessageBoxButtons buttonTyperegistered = MessageBoxButtons.OK;
MessageBoxIcon iconTyperegistered = MessageBoxIcon.Information;
DialogResult resultregistered = MessageBox.Show("You've successfully registered to play!\n Your username is " + uname, "Altarian Chess", buttonTyperegistered, iconTyperegistered, 0, 0);
con.Close();
Form4 frm4 = new Form4();
frm4.Show();
frm4.Visible = true;
Form3 frm3 = new Form3();
frm3.Hide();
frm3.Visible = false;
}
[/code]
Loading
yasPosted Mar 12, 2009, 3:52 PM
it doesnt give any errors..i thought it wasnt inserting but now i realized that vs 2005 has 2 database files..one in the projects (at the root level) and one in the bin folder...so its apparently inserting into the db file in the bin folder but showing me the one from the projects folder.
so what i did is in the Solution Explorer, i clicked on the database, and in the "Properties" section, for the part "CopyToOutputDirectory" i set it to "Do Not Copy". Then, in the Server Explorer, i right-clicked on the database and selected "Modify Connection...". from the "Browse..." button i selected the copy of the DB in the "bin" folder.
when i did this is seems to be working fine (although sometimes but not always, when i close then open visual studio it seems to delete the db file in the bin folder)...but even when it does work when i do this, when i close and open visual studios, the modify connection part goes back to the db file in the project (instead of the db file in the bin folder as i had set to)...
so i dont know how to get these two db files working consistently....? i couldnt seem to figure out whats happening
thank you!!
StefanPosted Mar 12, 2009, 3:24 PM