-2

I am creating one application my requirement is what when column name Status is N in Registration table, then current form should hide and Login form should be open.

If Status is not N then its should be open Registration_Form. I'm trying but it's causing

Error creating window handle

on the rf.Show() call.

on insert button code
 string status = "Y";
                //Random random = new Random();
                //int randomNumber = random.Next(0, 100);
                string random1 = System.Web.Security.Membership.GeneratePassword(10, 0);
                string concate = textBox1.Text + "-" + textBox2.Text + "-" + textBox3.Text.Substring(textBox3.Text.Length - 4) + "-" + random1;

                string connectionString = null;
                connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
                con.ConnectionString = connectionString;


                string SqlString = "Insert Into Registration (Name,Last_Name,Contact_No,Address,Insert_Date,Registration_key,Status) Values (?,?,?,?,?,?,?)";
                //using (OleDbCommand cmd = new OleDbCommand(SqlString, con))
                //{
                OleDbCommand cmd = new OleDbCommand(SqlString, con);
                con.Open();

                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("@Name", textBox1.Text);
                cmd.Parameters.AddWithValue("@Last_Name", textBox2.Text);
                cmd.Parameters.AddWithValue("@Contact_No", textBox3.Text);
                cmd.Parameters.AddWithValue("@Address", textBox4.Text);
                cmd.Parameters.AddWithValue("@Insert_Date", textBox5.Text);
                cmd.Parameters.AddWithValue("@Registration_key", concate);
                cmd.Parameters.AddWithValue("@Status", status);

                //}

                int n = cmd.ExecuteNonQuery();
                con.Close();
                if (n > 0)
                {
                    MessageBox.Show("Data Inserted Successfully,NOW PLEASE ACTIVATE APPLICATION PUTTING ACTIVATE KEY ", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
on update button code --
string Status = "N";
            string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;



            string recover = "SELECT Registration_key from Registration where Registration_key='" + textBox6.Text + "'";
            OleDbCommand cmd = new OleDbCommand(recover, con);
            con.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                textBox6.Text = reader["Registration_key"].ToString();

                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

                string cmd1 = "update Registration set Status=@Status where Registration_key=@Registration_key";
                 cmd = new OleDbCommand(cmd1, con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Status", Status);
                cmd.Parameters.AddWithValue("@Registration_key", textBox6.Text);

                con.Open();
                int n2 = cmd.ExecuteNonQuery();
                con.Close();
 this.Hide();

                Login_Page lp = new Login_Page();
                lp.Show();
}

          else

            {
                MessageBox.Show("Invalid Activated Key", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Stop);

            }
            con.Close();

on load event--
string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;
            string Comparing="N";

            string query = "select Status from Registration where Status='N'";
            con.Open();
            OleDbCommand cmd = new OleDbCommand(query, con);
            string compare = Convert.ToString(cmd.ExecuteScalar());
            con.Close();
            if (compare == Comparing)
            {
                this.Hide();

                Login_Page lp = new Login_Page();
                lp.Show();

            }
            else if (compare != Comparing)
            {
                Registration_Form rf = new Registration_Form();
                rf.Show();
            }

enter image description here enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Atul
  • 155
  • 1
  • 1
  • 19
  • is this code executing on a form itself? if so, what form? – BackDoorNoBaby Mar 10 '17 at 16:27
  • Possible duplicate of [Winforms issue - Error creating window handle](http://stackoverflow.com/questions/222649/winforms-issue-error-creating-window-handle) – BackDoorNoBaby Mar 10 '17 at 16:29
  • what if you have more than 1 record set to `N` how are you keeping track of the current user if their registration Status = `N` start with using the debugger also what does the code look like for the `Registration_Form` can you show that code..? perhaps you are missing a call to `InitializeComponent();` – MethodMan Mar 10 '17 at 16:34
  • @BackDoorNoBaby : this code executing on Registration_Form on Registration_Form_Load event – Atul Mar 10 '17 at 16:35
  • @MethodMan : the code is like if he found status N then redirected to login page so there is no chances to add any different records so only one record should user by default status is Y after user detail enter it become N – Atul Mar 10 '17 at 16:38
  • you need to show all relevant code.. what you respond to in your comment is not enough.. also are you familiar with `MDI` there are so many ways you can handle this but what you have posted in code is not enough.. you should have a separate class to handle the Login, if not valid user then redirect to a Registration page.. – MethodMan Mar 10 '17 at 16:42
  • @MethodMan : i added my whole code with images for references – Atul Mar 10 '17 at 16:52

1 Answers1

0

i got a solution i remove e

lse if (compare != Comparing)
            {
                Registration_Form rf = new Registration_Form();
                rf.Show();
            } 

this and instead that normal use else condition

connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;
            string Comparing="N";

            string query = "select Status from Registration where Status='N'";
            con.Open();
            OleDbCommand cmd = new OleDbCommand(query, con);
            string compare = Convert.ToString(cmd.ExecuteScalar());
            con.Close();
            if (compare == Comparing)
            {
                this.Hide();

                Login_Page lp = new Login_Page();
                lp.Show();

            }
            else 
{
MessageBox.Show("Pls Register yourself");

}

this code giving me what requirement i want

Atul
  • 155
  • 1
  • 1
  • 19