Reputation: 736
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();
}
Upvotes: 0
Views: 21702
Reputation: 7961
You do not have to use DataTable.
Assuming there is only one row returned you can do that:
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();
if (!reader.Read())
return;
cmbQuotationcode.ValueMember = "quotationpk";
cmbQuotationcode.DisplayMember = "quotationcode";
cmbQuotationcode.DataSource = reader;
txtamount.text = reader["amount"].ToString();
oleDbConnection1.Close();
}
Upvotes: 0
Reputation: 18843
have you tried getting at the table.Rows?
DataTable dt = new DataTable();
DataRow row = table.Rows[0];
dt.Columns.Add("quotationpk", typeof(int));
dt.Columns.Add("quotationcode", typeof(string));
dt.Columns.Add("amount", typeof(int));
//then you could assign the textbox like this
txtamount.text= (string)row["amount"];
Something like this should lead you to the correct answer also are you always expecting to get only 1 amount..? if not then you need to wrap that code in a loop..
* Personally I would use an OleDbDataReader it will read the column and fields instead of having to add the fields like you are..
here is an example of how you could use the OleDbDataReader I have a method I have written to GetNames from a DataBase for example
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();
reader.Read();
cmbQuotationcode.ValueMember = "quotationpk";
cmbQuotationcode.DisplayMember = "quotationcode";
cmbQuotationcode.DataSource = reader;
txtamount.text = reader["amount"].ToString();
oleDbConnection1.Close();
}
Upvotes: 1
Reputation: 18290
create dt at class scope
DataTable dt = new DataTable();
/// Add columns to Table at Form_Load()
dt.Columns.Add("quotationpk", typeof(int));
dt.Columns.Add("quotationcode", typeof(string));
dt.Columns.Add("amount", typeof(int));
//Then do fill operation
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();
dt.Load(reader);
cmbQuotationcode.ValueMember = "quotationpk";
cmbQuotationcode.DisplayMember = "quotationcode";
cmbQuotationcode.DataSource = dt.DefaultView;
oleDbConnection1.Close();
}
//filter data and display in the text box
private void cmbQuotationcode_SelectedIndexChanged(object sender, EventArgs e)
{
DataView dvDataTable = new DataView(dt);
dvDataTable.RowFilter = "quotationpk ='" + cmbQuotationcode.SelectedValue "'";
if(dvDataTable.Count > 0)
{
txtamount.Text= Convert.ToString(dvDataTable["amount"]);
}
else
{
txtamount.Text = "0";
}
}
Upvotes: 1