I have a program which writes to an Access data table, and then the information is displayed in the data grid. this is triggered when a 6 digit number occurs in the textbox followed by an "L"
. I have "TextBox1.Clear();"
in the code which clears the 6 numbers away, but not the "L"
.
Here is my code:
private void Registration_Main(object sender, KeyPressEventArgs e)
{
SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
Object returnValue;
Object returnName;
string txtend = textBox1.Text;
if (e.KeyChar == 'L')
{
DBConnection.Open();
}
if (DBConnection.State == ConnectionState.Open)
{
if (textBox1.Text.Length != 6) return;
{
cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id =@Name");
cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", "")));
cmd.CommandType = CommandType.Text;
cmd.Connection = DBConnection;
returnValue = cmd.ExecuteScalar() + "\t " + textBox1.Text.Replace(@"L", "");
returnName = cmd.ExecuteScalar();
DBConnection.Close();
AccessDB_Connection(returnName);
}
textBox1.Clear();
}
}
private void AccessDB_Connection(object returnName)
{
String varID;
Object varName;
varID = textBox1.Text.Replace(@"L", "").ToString();
varName = returnName;
OleDbConnection OLEDB_Connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Test Applications\\Tablet Registration - Access Database\\Tablet Registration - Access\\Tablet Registration - Access\\Registration.accdb");
OleDbCommand updateCmd = new OleDbCommand();
try
{
updateCmd.CommandText = "INSERT INTO TestDB ([Name], [ID]) VALUES (@NAME, @ID)";
updateCmd.Parameters.AddWithValue("@NAME", varName);
updateCmd.Parameters.AddWithValue("@ID", varID);
OLEDB_Connection.Open();
updateCmd.Connection = OLEDB_Connection;
updateCmd.ExecuteNonQuery();
this.testDBTableAdapter.Fill(this.registrationDataSet1.TestDB);
OLEDB_Connection.Close();
}
catch
{
}
}
Any help is much appreciated !
Try updating your code from:
if (e.KeyChar == 'L')
{
DBConnection.Open();
}
to
if (e.KeyChar == 'L')
{
e.Handled = true;
DBConnection.Open();
}
This should stop the event propagating and populating your textbox.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.