Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my windows form i have two combobox and one text box when a jobecode is selected from cmbjobecode it will load cmbquotationcode with corresponding quotations and fills a textbox txtamount with amount of selected quotation

all is fine except i cannot get the textbox filled with the amount can anyone help in sorting mistake


 private void cmbjobcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboQuotationboxload();
        }

public void comboQuotationboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();

            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from  quotationmastertable  where jobpk = " + cmbjobcode.SelectedValue + "", oleDbConnection1);



            OleDbDataReader reader = oleDbCommand1.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Columns.Add("quotationpk", typeof(int));
            dt.Columns.Add("quotationcode", typeof(string));
            dt.Columns.Add("amount", typeof(int));
            dt.Load(reader);
            cmbQuotationcode.ValueMember = "quotationpk";
            cmbQuotationcode.DisplayMember = "quotationcode";
            cmbQuotationcode.DataSource = dt.DefaultView;
            txtamount.text= "amount";


            oleDbConnection1.Close();

        }
Posted
Comments
Sergey Alexandrovich Kryukov 23-Feb-12 14:20pm    
Not a question. Not clear. What mistake? What is expected?
--SA

First things first. You should REALLY use Parameterized Queries! What if the selected value of cmrjobcode was something like ; --drop table users :)
You can do this as follows:
C#
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from  quotationmastertable  where jobpk = @jobpk", oleDbConnection1);
oleDbCommand1.Parameters.AddWithValue("@jobpk", cmbjobcode.SelectedValue);
This will replace any parameter in your query string (in this case @jobpk) with the value of the object provided to the AddWithValue Method[^] of your Parameter collection of your Command[^].
Your code is now better to read, your query will possibly execute faster (parameterized queries have some performance gain) and your code is Sql Injection[^] safe!
Second, you should really dispose of your objects if they implement IDisposable[^]. This is the case for you connection and command objects. You can do this using the using keyword[^] or by calling oleDbConnection1.Dispose and oleDbCommand1.Dispose[^].
Also, you should better name your variables and objects. For example, cmbjobcode is hard to read. cmbJobCode reads much better!

Now to answer your question, you are currently simply setting the text to the literal string "amount". What I am sure you wish to do is put the amount of the currently selected DataRow [^]from your DataTable[^] in the TextBox.Text[^].
What you should probably do is create a BindingSource[^] and assign your DataTable to the DataSource Property[^]. You can now set the DataSource of your ComboBox to point at the BindingSource and you can add a Binding[^] to your TextBox using TextBox.DataBindings[^] (Inherited from Control[^]). You can set most of the properties in your designer.
In your code just add:
C#
bindingSource.DataSource = dt;
I think that should do the trick. Good luck! :)
 
Share this answer
 
Comments
Oshtri Deka 23-Feb-12 14:48pm    
Naerling can you please do some formatting. It is end of a long day and I am having trouble reading your answer.
It's nothing personal, but you use several fore-colors and your choice of paragraph formatting doesn't help.
Cheers!
Sander Rossel 23-Feb-12 15:55pm    
Red for variable names, blue for unvisited links, purple for visited links, black for everything else. The [^] at the end of the links is to open them in a new windows. Not to sound rude or cocky, but I have read and written enough CP posts, answers and articles to know formatting on CP, and this is it. If you can't read it that seems like your problem...
finally I did it

txtamount.text= dt.Rows[0]cells[2].Tostring();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900