Join 136,075 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,598 people online right now. Registration is fast and FREE... Join Now!
well i am making a inteface in visual c# but when i insert data in database i want to check the code and the name when i type code in database firstly it search in database wheather the data is exsisting or not..i want to use auto text mode feature in text box ..when user type code in text box...then automatically show tha existing code or name in auto text mode property..then user select what he want or insert confusion become less......here is example plz help for that
CODE
private void txtcode_KeyPress(object sender, KeyPressEventArgs e) { if ((int)e.KeyChar == 13) { if (((txtcode.Text == "") || ((txtcode.TextLength) > 4) || ((txtcode.TextLength) < 1))) { MessageBox.Show("Enter value >= 4 character or <= 1 character", "Code Error....", MessageBoxButtons.OK, MessageBoxIcon.Error); txtcode.Focus(); txtcode.SelectionStart = 0; txtcode.SelectionLength = txtcode.TextLength; } else { string strcode = txtcode.Text; OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\VC\\dbmedicine.mdb"); con.Open(); OleDbCommand cmd2 = new OleDbCommand("select count(m_code) from medicine where m_code='" + strcode + "'", con); OleDbCommand cmd1 = new OleDbCommand("select medicine.*,s_name from medicine left outer join agency on medicine.s_code=agency.s_code where m_code='" + strcode + "'", con); OleDbDataReader dataread = cmd1.ExecuteReader(); int total = (int)cmd2.ExecuteScalar(); if (total > 0) { MessageBox.Show("Data are already Exist in database", "Data Information", MessageBoxButtons.OK, MessageBoxIcon.Information); while (dataread.Read()) { txtcode.Text = Convert.ToString(dataread["m_code"]); txtmname.Text = Convert.ToString(dataread["m_name"]); cbotype.Text = Convert.ToString(dataread["m_type"]); txtprice.Text = Convert.ToString(dataread["m_price"]); txtmpsize.Text = Convert.ToString(dataread["m_p_size"]); txtmpunit.Text = Convert.ToString(dataread["m_p_unit"]); txtminqty.Text = Convert.ToString(dataread["min_qty"]); txtroqty.Text = Convert.ToString(dataread["re_o_qty"]); txtrackno.Text = Convert.ToString(dataread["rack_no"]); txtstoreno.Text = Convert.ToString(dataread["store_no"]); cboscode.Text = Convert.ToString(dataread["s_code"]); lblsname.Text = Convert.ToString(dataread["s_name"]);
I'm not entirely sure I follow the question. The code seems fine, if messy. For instance you don't need two database calls. Are you looking for a code refactoring?
If so, here's one possible option. Note the use of methods to make the main code less cluttered.
csharp
private void txtcode_KeyPress(object sender, KeyPressEventArgs e) { if ((int)e.KeyChar != 13) { return; } if (!ValidateTxtcode()) { return; }
if (LoadData(txtcode.Text)) { locktext(); btnedit.Enabled = true; btndelete.Enabled = true; btnclear.Focus(); } else { MessageBox.Show("Data is Not Existing in Database", "Data Information...", MessageBoxButtons.OK, MessageBoxIcon.Information); txtcode.Text = txtcode.Text.ToUpper(); txtcode.ReadOnly = true; txtmname.ReadOnly = false; txtmname.Focus(); } // found in both results btnclear.Enabled = true; cboscode.Enabled = false; cbotype.Enabled = false; }
private bool LoadData(string strcode) { bool dataLoaded = false; OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\VC\\dbmedicine.mdb"); OleDbCommand cmd1 = new OleDbCommand("select" + " medicine.*, s_name" + " from medicine left outer join agency on medicine.s_code=agency.s_code" + " where medicine.m_code='" + strcode + "'", con ); try { con.Open(); OleDbDataReader dataread = cmd1.ExecuteReader(); // we're only expecting one result // if this is true, we got it, otherwise it doesn't exist if (dataread.Read()) { txtcode.Text = Convert.ToString(dataread["m_code"]); txtmname.Text = Convert.ToString(dataread["m_name"]); cbotype.Text = Convert.ToString(dataread["m_type"]); txtprice.Text = Convert.ToString(dataread["m_price"]); txtmpsize.Text = Convert.ToString(dataread["m_p_size"]); txtmpunit.Text = Convert.ToString(dataread["m_p_unit"]); txtminqty.Text = Convert.ToString(dataread["min_qty"]); txtroqty.Text = Convert.ToString(dataread["re_o_qty"]); txtrackno.Text = Convert.ToString(dataread["rack_no"]); txtstoreno.Text = Convert.ToString(dataread["store_no"]); cboscode.Text = Convert.ToString(dataread["s_code"]); lblsname.Text = Convert.ToString(dataread["s_name"]); dataLoaded = true; } } finally { con.Close(); } return dataLoaded; }
If I totally missed what you're looking for, please try to explain again. Communicating and understanding a problem is always a challenge, particularly with computers.
I'm not entirely sure I follow the question. The code seems fine, if messy. For instance you don't need two database calls. Are you looking for a code refactoring?
If so, here's one possible option. Note the use of methods to make the main code less cluttered.
csharp
private void txtcode_KeyPress(object sender, KeyPressEventArgs e) { if ((int)e.KeyChar != 13) { return; } if (!ValidateTxtcode()) { return; }
if (LoadData(txtcode.Text)) { locktext(); btnedit.Enabled = true; btndelete.Enabled = true; btnclear.Focus(); } else { MessageBox.Show("Data is Not Existing in Database", "Data Information...", MessageBoxButtons.OK, MessageBoxIcon.Information); txtcode.Text = txtcode.Text.ToUpper(); txtcode.ReadOnly = true; txtmname.ReadOnly = false; txtmname.Focus(); } // found in both results btnclear.Enabled = true; cboscode.Enabled = false; cbotype.Enabled = false; }
private bool LoadData(string strcode) { bool dataLoaded = false; OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\\VC\\dbmedicine.mdb"); OleDbCommand cmd1 = new OleDbCommand("select" + " medicine.*, s_name" + " from medicine left outer join agency on medicine.s_code=agency.s_code" + " where medicine.m_code='" + strcode + "'", con ); try { con.Open(); OleDbDataReader dataread = cmd1.ExecuteReader(); // we're only expecting one result // if this is true, we got it, otherwise it doesn't exist if (dataread.Read()) { txtcode.Text = Convert.ToString(dataread["m_code"]); txtmname.Text = Convert.ToString(dataread["m_name"]); cbotype.Text = Convert.ToString(dataread["m_type"]); txtprice.Text = Convert.ToString(dataread["m_price"]); txtmpsize.Text = Convert.ToString(dataread["m_p_size"]); txtmpunit.Text = Convert.ToString(dataread["m_p_unit"]); txtminqty.Text = Convert.ToString(dataread["min_qty"]); txtroqty.Text = Convert.ToString(dataread["re_o_qty"]); txtrackno.Text = Convert.ToString(dataread["rack_no"]); txtstoreno.Text = Convert.ToString(dataread["store_no"]); cboscode.Text = Convert.ToString(dataread["s_code"]); lblsname.Text = Convert.ToString(dataread["s_name"]); dataLoaded = true; } } finally { con.Close(); } return dataLoaded; }
If I totally missed what you're looking for, please try to explain again. Communicating and understanding a problem is always a challenge, particularly with computers.
Hope this helps.
Thx for your reply but i want to know when i enter code in text box that textbox automatically show the code and name that are existing in database when user press the key with particular character if the code is existing that show in dropdown list with code and name other wise it accpet the code and go to next texbox.(My concentration on Autocomplete textbox it is possible
Oh, I think you're referring to something like AJAX's autocomplete. You can try wiring an Event to handle every keypress or change in your textbox. And in that event you start querying your database for possible entries.
But wouldn't this bog down your database? You should cache some of the results to improve performance.
hey i want hotel managemnet system in C# plzz help me guys
We will help you, but we won't do the work for you. What have you done so far?
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
how to validate masked thex box well i am using masked textbox and i wnat to limit the entey like that i have phone masked control well when user didn't type in maked text box then cursor will go to next control if user type in masked text box he/she must enter the data not less than given value.plz help