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:
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:
bindingSource.DataSource = dt;
I think that should do the trick. Good luck! :)