Короче, я попробовал решения из другого поста, но не могу заставить его работать.Я новичок в программировании, и мне дали задание из моей работы по созданию программного обеспечения.
Я создал две формы, в которой первая форма называется AddNewUser, а другая - RegistrationPopOut, где последняя форма будетвсплывающее окно, если пользователь нажал «Зарегистрироваться» в форме AddNewUser.
Я хочу, чтобы представление данных в AddNewUser автоматически обновлялось само по себе при нажатии «save» на RegistrationPopOut
Вот код для AddNewUser:
private void Register_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || comboBox1.Text == "" || comboBox2.Text == "" || comboBox3.Text == "")
{
MessageBox.Show("Please fill in every fields");
}
else
{
int i = 0;
SqlCommand cmd = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
cmd.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted.
cmd.CommandText = "select * from registration where username='" + textBox3.Text + "' or idno='" + textBox2.Text + "'";
//cmd.CommandText = "select * from registration where username='" + textBox3.Text + "'";
cmd.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());
if (i == 0) //If ok nada masalah register
{
SqlCommand cmd1 = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
cmd1.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted.
cmd1.CommandText = "insert into registration values('" + textBox3.Text + "','" + textBox4.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + comboBox3.Text + "')";
cmd1.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; comboBox1.Text = ""; comboBox2.Text = ""; comboBox3.Text = "";
display();
MessageBox.Show("Successfully registered");
}
else //if ada masalah register
{
MessageBox.Show("User already registered");
}
}
}
private void display()
{
SqlCommand cmd = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
cmd.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted.
cmd.CommandText = "select IDNO as 'Employee ID',Username,Name as 'Full Name',Istana,Position,Area from registration";
//cmd.CommandText = "select * from registration where username='" + textBox3.Text + "'";
cmd.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void AddNewUser_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open) //apa maksudnya?
{
con.Close();
}
con.Open();
display();
}
private void button2_Click(object sender, EventArgs e)
{
Employees.RegistrationPopOut RegPO = new Employees.RegistrationPopOut();
RegPO.Show();
display();
}
Вот код для RegisterPopOut
private void BtnRegister_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || comboBox1.Text == "" || comboBox2.Text == "" || comboBox3.Text == "")
{
MessageBox.Show("Please fill in every fields");
}
else
{
int i = 0;
SqlCommand cmd = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
cmd.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted.
cmd.CommandText = "select * from registration where username='" + textBox3.Text + "' or idno='" + textBox2.Text + "'";
//cmd.CommandText = "select * from registration where username='" + textBox3.Text + "'";
cmd.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());
if (i == 0) //If ok nada masalah register
{
SqlCommand cmd1 = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
cmd1.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted.
cmd1.CommandText = "insert into registration values('" + textBox3.Text + "','" + textBox4.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + comboBox3.Text + "')";
cmd1.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; comboBox1.Text = ""; comboBox2.Text = ""; comboBox3.Text = "";
MessageBox.Show("Successfully registered");
}
else //if ada masalah register
{
MessageBox.Show("User already registered");
}
}
}
Я поставил display ();в AddNewUser, чтобы он обновлялся, но я не работал.
Большое спасибо!