Textbox to display value based on combobox(windows applications)? [Resolved]

Posted by Sudheep.grandhe under C# on 12/1/2011 | Points: 10 | Views : 7624 | Status : [Member] | Replies : 10
Hi,

On my main form there is a field called Companyname. This field uses a combox called cmb_companyname to select Customer Names from a table called tbl_Customer.

Further down my form I want to have several textboxes which displays relevant information to the selected Customer like Company Name, Address,invoiceno,orderno,ondate,invoicedate,particulars,amountstatus,quantity,perprice,amount,netamount,netvat,tamount etc.

What should I put in the control source for the text box? I have something like code here.please do the needful.

Tbl_report1 columns:
Invoiceno,particulars,quantity,perprice,amount,invoicedate,ondate,invoiceid,orderno
Tbl_report2 columns:
Compname,compaddress,invoiceno(primarykey),orderno,ondate,invoicedate,amountstatus,netamount,netvat,tamount
Tbl_customer columns:
Custid(primarykey),custname,custaddress,custcity,custstate,custstdcode,custphoneno,custmobileno

private void loadcompanies()
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select '0' as compaddress,'selectone' as compname union select compaddress,compname from dbo.tbl_report2 ", con);//I am using here customer table.for this reason only it shows customer address itself.can you please correct this for getting all fields.
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
da.Fill(dt);
cmb_companyname.DataSource = dt;
cmb_companyname.DisplayMember = "compname";
cmb_companyname.ValueMember = "compaddress";
}
But it shows the customeraddressonly.for that I wrote the code in selectedindexchangedevent.
private void cmb_companyname_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select t2.compaddress,t1.invoiceno,t1.orderno,t1.ondate,t1.particulars,t1.perprice,t1.amount,t2.netvat,t2.tamount,t2.netamount from tbl_report2 t2,tbl_report1 t1 where t1.compname='hdfcbank' and t2.compname='hdfcbank'", con);
//SqlCommand cmd = new SqlCommand("select custaddress from tbl_customer where custname=@custname", con);
//cmd.Parameters.AddWithValue("@custname",cmb_companyname .Text );
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
txt_customeraddress.Text = cmb_companyname.SelectedValue.ToString();
//txt_orderno.Text = cmb_companyname.SelectedValue.ToString();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txt_customeraddress .Text =dt .Rows [0]["customeraddress"].ToString ();
//txt_customeraddress.Text = dt.Rows[0][0].ToString();
txt_invoiceno.Text = dt.Rows[0][1].ToString();
txt_orderno.Text = dt.Rows[0][2].ToString();
txt_ondate.Text = dt.Rows[0][3].ToString();
cmb_particulars.Text = dt.Rows[0][4].ToString();
txt_quantity.Text = dt.Rows[0][5].ToString();
txt_perprice.Text = dt.Rows[0][6].ToString();
txt_amount.Text = dt.Rows[0][7].ToString();
txt_vat.Text = dt.Rows[0][8].ToString();
txt_totalamount.Text = dt.Rows[0][9].ToString();
txt_netamount.Text = dt.Rows[0][10].ToString();
}

}
}

Best,
Sudheep.



Responses

Posted by: Sksingh on: 12/13/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,

try below code for date time columns

if (txt_invoicedate.Text != null && txt_invoicedate.Text.Trim() != "") 

{
cmd.Parameters.AddWithValue("@invoicedate", Convert.ToDateTime(txt_invoicedate.Text));
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DbNull.Value);
}
if (txt_ondate.Tex != null && txt_ondate.Tex.Trim() != "")
{
cmd.Parameters.AddWithValue("@ondate", Convert.ToDateTime(txt_ondate.Text));
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nilschaudhari on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
what is your question in that you done the right thing

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,
the previous problem was resolved.here is my issue.

Here is my requirment like i have gridview on my form in that if the user enter all the details it should display in gridview.unfortunately user enter the wrong details.if he wants to correct or delete the records in gridview only those records affected in db also.in gridview itself i have to display the buttons like update,delete how can i do this.(windows applications)


Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

I am not sure how it works in winform but one simple solution for your requirement.
Step1: Let display all records in gridview
step2: keep outsider button "Modify"

So if user want to modify any records from grid then they have to select one row and click on modify button , the moment they click on modify button the selected gridview row data should be bind in same manner in your entry form UI.

from there u can update or delele and every update and delete rebind your datagrid.
It will solve your problem easy way...


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

as you said it is correct only.But,my issue is the user creating his newinvoice if he enters the data it will displayed in the gridview.after that he will check whether data is correct or not .if data is wrong he has to modify using modify button.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,


it shows the following erro-.Arithmetic overflow error converting expression to data type datetime.
The statement has been terminated.

private void btn_add_Click(object sender, EventArgs e)
{
char sepchar = '/';
string strondate, strondate1, ondate;
string strinvoicedate, strinvoicedate1, invoicedate2;
//SqlConnection con = new SqlConnection("data source=mars-104;intial catalog=marsweb.imageimpacts;integrated security=true");
DBConnection db = new DBConnection();

SqlConnection con = db.opencon();
SqlCommand cmd = new SqlCommand("insert into tbl_temp (invoiceno,particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid)values(@invoiceno,@particulars,@quantity,@perprice,@amount,@orderno,@compname,@invoicedate,@ondate,@invoiceid)", con);
cmd.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@particulars", cmb_particulars.Text);
cmd.Parameters.AddWithValue("@quantity", txt_quantity.Text);
cmd.Parameters.AddWithValue("@perprice", txt_perprice.Text);
cmd.Parameters.AddWithValue("@amount", txt_amount.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);

strinvoicedate = txt_invoicedate.Text;
if (strinvoicedate != "null")
{
char[] separator = new char[] { ' ' };
string[] s1 = strinvoicedate.Split(separator);
string date2 = s1[0].ToString();
Console.WriteLine(date2);
if (date2 != "null")
{
if (date2.IndexOf('/') > 0)
{
sepchar = '/';
}
else if (date2.IndexOf('/') > 0)
{
sepchar = '/';
}
char[] separator1 = new char[] { '/' };
string[] s2 = date2.Split(separator1);
string day = s2[0].ToString();
string month = s2[1].ToString();
string year = s2[2].ToString();
strinvoicedate1 = (month + "/" + day + "/" + year);
invoicedate2 = strinvoicedate1;
cmd.Parameters.AddWithValue("@invoicedate", invoicedate2);
}
}
strondate = txt_ondate.Text;
if (strondate != "null")
{
char[] separator = new char[] { ' ' };
string[] s1 = strondate.Split(separator);
string date1 = s1[0].ToString();
if (date1 != "null")
{
if (date1.IndexOf('/') > 0)
{
sepchar = '/';
}
else if (date1.IndexOf('/') > 0)
{
sepchar = '/';
}
char[] separator1 = new char[] { '/' };
string[] s2 = date1.Split(separator1);
string day = s2[0].ToString();
string month = s2[1].ToString();
string year = s2[2].ToString();
strondate1 = (month + "/" + day + "/" + year);
ondate = strondate1;
cmd.Parameters.AddWithValue("@ondate", ondate);
}
}
cmd.Parameters.AddWithValue("@invoiceid", txt_invoiceno.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();//error comes here as Arithmetic overflow error converting expression to data type datetime.
The statement has been terminated.
con.Close();
txt_quantity.Text = "";
txt_perprice.Text = "";
txt_amount.Text = "";
netamount();
loadparticularsdata();
vatamount();
griddisplay();
}


Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

for updating which code i have to write .

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Simple write update commmand instead of insert command and other will be same.

Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi i wrote this code for updating.
public partial class NewInvoice : Form
{
private String connectionString = null;
private SqlConnection sqlConnection = null;
private SqlDataAdapter sqlDataAdapter = null;
private SqlCommandBuilder sqlCommandBuilder = null;
private DataTable dataTable = null;
private BindingSource bindingSource = null;
private String selectQueryString = null;


private void NewInvoice_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
selectQueryString = "update tbl_temp set invoiceno=@invoiceno";
conn.Open();
sqlDataAdapter = new SqlDataAdapter(selectQueryString, conn);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable );
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
GridView1.DataSource = bindingSource;
}
private void btn_update_Click_1(object sender, EventArgs e)
{
try
{
sqlDataAdapter.Update(dataTable);
MessageBox.Show("updated successfully");
}
catch (Exception exceptionobj)
{
MessageBox.Show(exceptionobj.Message.ToString());
}
}

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: chaudharysunny1989-22042 on: 5/8/2013 [Member] Starter | Points: 25

Up
0
Down
i have done it but there is a problem if there r 2 or 3 party with same name then it take only address only for 1st party name not for 2nd or 3rd what should be done
my code is here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Inventry_Software
{
public partial class Sale_Bill : Form
{
string ConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\inventdata.accdb;Persist Security Info=True;User ID=admin;Jet OLEDB:Database Password=admin";
public Sale_Bill()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
this.Hide();
var myForm = new Add_Invoic();
myForm.Show();

}

//form load
private void Sale_Bill_Load(object sender, EventArgs e)
{
using (OleDbConnection con = new OleDbConnection(ConString))
{
OleDbCommand cmd = new OleDbCommand("SELECT party_name FROM account_setup", con);
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
while (reader.Read())
{
comboBox1.Items.Add(reader[0].ToString());
MyCollection.Add(reader[0].ToString());
}
comboBox1.AutoCompleteCustomSource = MyCollection;
con.Close();
}
}
//party_name
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// label8.Text=
OleDbConnection con = new OleDbConnection(ConString);
OleDbCommand cmd = new OleDbCommand("select address from account_setup where Party_name='" + comboBox1.SelectedItem + "'", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
textBox1.Text = dt.Rows[0]["address"].ToString();
}
}

//to date
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
string str = dateTimePicker2.Text;
DateTime dd = Convert.ToDateTime(str);
textBox2.Text = dd.ToString("dd-MMM-yyyy");
}
//from date
private void dateTimePicker1_ValueChanged_1(object sender, EventArgs e)
{
string str = dateTimePicker1.Text;
DateTime dd = Convert.ToDateTime(str);
textBox3.Text = dd.ToString("dd-MMM-yyyy");
}
}
}


Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response