yas

yas

  • NA
  • 8
  • 0

cant insert into my database

Mar 9 2009 5:18 PM
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]

Answers (2)