Welcome to Dream.In.Code
Getting C# Help is Easy!

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!




Visual C# and Data base help

 
Reply to this topicStart new topic

Visual C# and Data base help

lookfahim
13 Apr, 2008 - 10:36 AM
Post #1

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 4

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"]);

                        }
                        dataread.Close();
                        con.Close();
                        locktext();
                        btnclear.Enabled = true;
                        btnedit.Enabled = true;
                        btndelete.Enabled = true;
                        cboscode.Enabled = false;
                        cbotype.Enabled = false;
                        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();
                        btnclear.Enabled = true;
                        cboscode.Enabled = false;
                        cbotype.Enabled = false;
                    }
                }
            }
        }

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Visual C# And Data Base Help
13 Apr, 2008 - 01:03 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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 ValidateTxtcode() {
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;
return false;
} else {
return true;
}
}


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. wink2.gif

Hope this helps.


User is online!Profile CardPM
+Quote Post

lookfahim
RE: Visual C# And Data Base Help
13 May, 2008 - 02:14 AM
Post #3

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 4

QUOTE(baavgai @ 13 Apr, 2008 - 02:03 PM) *

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 ValidateTxtcode() {
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;
return false;
} else {
return true;
}
}


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. wink2.gif

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
User is offlineProfile CardPM
+Quote Post

thor78
RE: Visual C# And Data Base Help
13 May, 2008 - 08:01 PM
Post #4

D.I.C Head
Group Icon

Joined: 6 May, 2008
Posts: 106


Dream Kudos: 50
My Contributions
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.

HTH. smile.gif
User is offlineProfile CardPM
+Quote Post

yesra
RE: Visual C# And Data Base Help
16 May, 2008 - 12:01 PM
Post #5

New D.I.C Head
*

Joined: 16 May, 2008
Posts: 1

hey i want hotel managemnet system in C# plzz help me guys
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Visual C# And Data Base Help
16 May, 2008 - 12:24 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
QUOTE(yesra @ 16 May, 2008 - 01:01 PM) *

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.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

lookfahim
RE: Visual C# And Data Base Help
22 May, 2008 - 08:58 AM
Post #7

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 4

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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:20PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month