[英]Passing user login data from form1 to form2 listview using access database
I have created a program that needs to pass the employee number login data (Log_in) to the 2nd form (form1). 我创建了一个程序,该程序需要将员工编号登录数据(Log_in)传递给第二个表单(form1)。 I already created the database that consist of 3 tables: Employee_Information, SN_Incoming, and Duplicate.
我已经创建了包含3个表的数据库:Employee_Information,SN_Incoming和Duplicate。 I want the employee number found in the Employee Information move over to the SN_Incoming table.
我希望在“员工信息”中找到的员工编号移至SN_Incoming表。 That data will then be displayed in a listview.
然后,该数据将显示在列表视图中。
Login Form Code: 登录表单代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Serial_Number_Checker
{
public partial class Log_In : Form
{
public Log_In()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("Please enter your employee #!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
return;
}
try
{
Duplicate_Serial_Number_CheckerDataSetTableAdapters.Employee_InformationTableAdapter Employee_Number = new Duplicate_Serial_Number_CheckerDataSetTableAdapters.Employee_InformationTableAdapter();
Duplicate_Serial_Number_CheckerDataSet.Employee_InformationDataTable dt = Employee_Number.GetDataByEmployee_Number(textBox1.Text);
if (dt.Rows.Count > 0)
{
MessageBox.Show("You have been successfully logged in.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); // Process your login here
this.Hide();
Form1 form1 = new Form1();
if (form1.ShowDialog() != DialogResult.OK)
Application.Exit();
}
else
{
MessageBox.Show("Your Employee # is not registered. Please contact your Supervisor to be registered.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
button1.PerformClick();
}
}
} }
Form1 Code: Form1代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OleDb;
namespace Serial_Number_Checker
{
public partial class Form1 : Form
{
static string conString= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=";
OleDbConnection con = new OleDbConnection(conString);
OleDbCommand cmd;
OleDbDataAdapter adapter;
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
listView1.SelectedIndexChanged += new EventHandler (listView1_SelectedIndexChanged); // Adding columns to listView1
// list view properities
listView1.View = View.Details;
listView1.FullRowSelect = true;
// Add Columns
listView1.Columns.Add("Employee #", 150);
listView1.Columns.Add("Serial Number", 150);
listView1.Columns.Add("Date/Time", 150);
}
// Add Row
private void InsertSerialNumber(string sn)
{
using (OleDbCommand odc = new OleDbCommand("INSERT INTO SN_Incoming (SN) VALUES(@SN)", con))
{
odc.Parameters.AddWithValue("@SN", sn);
try
{
con.Open();
odc.ExecuteNonQuery();
}
catch (Exception e) { MessageBox.Show(e.Message);
}
finally { con.Close();
}
}
}
// Add To ListView1
private void populate(String employeeid, String sn, String timestamp)
{
// Row
String[] row = { employeeid, sn, timestamp };
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);
}
private void clearTxts()
{
textBox1.Text = "";
}
// Retrieve Check In
private void LoadSerialNumbers() {
listView1.Items.Clear();
DataTable dt = new DataTable();
using (OleDbCommand odc = new OleDbCommand("SELECT * FROM SN_Incoming", con)) {
try {
con.Open();
using (OleDbDataAdapter oda = new OleDbDataAdapter(odc))
oda.Fill(dt);
} catch (Exception e) { MessageBox.Show(e.Message); } finally { con.Close(); }
}
List<ListViewItem> items = new List<ListViewItem>();
foreach (DataRow row in dt.Rows) {
ListViewItem lvi = items.SingleOrDefault(s => s.Tag == row[1].ToString());
if (lvi != null)
continue;
lvi = new ListViewItem(new string[] { row[0].ToString(), row[1].ToString(), row[2].ToString() });
lvi.Tag = row[1].ToString();
items.Add(lvi);
}
listView1.Items.AddRange(items.ToArray());
} }
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
{
foreach (ListViewItem lvi in listView1.SelectedItems)
listView1.Items.Remove(lvi);
if (string.IsNullOrWhiteSpace(textBox1.Text))
MessageBox.Show("Please enter a serial number!", "Input");
else
InsertSerialNumber(textBox1.Text);
textBox1.Text = string.Empty;
textBox1.Focus();
textBox1.SelectionStart = textBox1.Text.Length == 0 ? 0 : textBox1.Text.Length - 1;
textBox1.SelectionLength = 0;
LoadSerialNumbers();
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
}
// If enter is pressed down during input, it will automatically submit
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(this, new EventArgs());
}
}
}
} }
I have already set my primary key to Employee_Number. 我已经将主键设置为Employee_Number。 I need to learn how to pass that information to the next table.
我需要学习如何将该信息传递到下表。 So I can retrieve and put in my listview.
这样我就可以检索并将其放入列表视图。 Your help is greatly appreciated.
非常感谢您的帮助。 Thanks
谢谢
Just add a parameter to the constructor of Form1
: 只需向
Form1
的构造函数添加一个参数:
public Form1(string id)
{
InitializeComponent();
//Do whatever you like with the passed-in id:
MessageBox.Show($"you passed me Id {id}");
}
To Use: 使用方法:
Form1 form1 = new Form1(TextBox1.Text);
if (form1.ShowDialog() != DialogResult.OK)
Application.Exit();
Though Generally, I'd advise moving all the DB stuff and 'Who's currently logged in' code to separate repository/service classes which are commonly available everywhere. 尽管通常来说,我还是建议将所有数据库内容和“谁当前登录的”代码移到单独的存储库/服务类中,这些存储类/服务类通常随处可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.