C# Datepicker value not saved in MySql database
-
Hi to All, I am using C# Winforms to create a Voucher system which is Master/Detail, using MySql as backend. Create Voucher class to setup followings: 1.Create New Voucher 2. Save Voucher 3. Query Voucher Currently having problem with Save Voucher, I've setup Insert/Update/Delete commands, to make this post short I am posting Insert commands so that general idea of application will be clear: Class Variables
ClsMgt da = new ClsMgt();
MySqlDataAdapter sqlDataMaster = new MySqlDataAdapter();
private DataSet oDs = null;
MySqlCommand selectcommand = null;
MySqlCommand insertcommand = null;
MySqlCommand updatecommand = null;
MySqlCommand deletecommand = null;
private DataTable dt = null;
private DataTable dtDet = null;
private String sSelProcName = null;
private String sInsProcName = null;
private String sDelProcName = null;
private String sUpdProcName = null;
private int voucType;public MySqlConnection oCn = new MySqlConnection();
MySqlTransaction oTrn = null;Following is the NewVoucher procedure which will setup VoucherForm to open with blank record:
public DataSet NewVoucher()
{
DataSet vDs = new DataSet();
oCn = da.GetConnection();if (oCn == null) { oCn.Open(); } try { DataTable dt = new DataTable(); //=============================================================================== //--- Set up the Select Command //===============================================================================
String sqlSelect = "Select vID, vTypeID, vNo, accCodeDR, accCodeCR, vDate, vChqNo, vChqDt, vPayName, vRemarks, vAmount from vMaster";
sqlDataMaster = new MySqlDataAdapter(sqlSelect, oCn);
sqlDataMaster.FillSchema(dt, SchemaType.Source);
vDs.Tables.Add(dt);
VoucherDetails vdet = new VoucherDetails();
DataTable dtDet = new DataTable();
dtDet = vdet.NewVoucherDet();vDs.Tables.Add(dtDet);
vDs.Tables[0].Columns["vID"].AutoIncrement = true;
vDs.Tables[0].Columns["vID"].AutoIncrementSeed = -1;
vDs.Tables[0].Columns["vID"].AutoIncrementStep = -1;
vDs.Tables[1].Columns["vID"].AutoIncrement = true;
vDs.Tables[1].Columns["vID"].AutoIncrementSeed = -1;
vDs.Tables[1].Columns["vID"].AutoIncrementStep = -1;
vDs.EnforceConstraints = false;
vDs.Relations.Add("VouchersToVoucherDetails", vDs.Tables[0].Columns["vID"], vDs.Tables[1].Columns["vID"]);} catch (MySqlException e) {
-
Hi to All, I am using C# Winforms to create a Voucher system which is Master/Detail, using MySql as backend. Create Voucher class to setup followings: 1.Create New Voucher 2. Save Voucher 3. Query Voucher Currently having problem with Save Voucher, I've setup Insert/Update/Delete commands, to make this post short I am posting Insert commands so that general idea of application will be clear: Class Variables
ClsMgt da = new ClsMgt();
MySqlDataAdapter sqlDataMaster = new MySqlDataAdapter();
private DataSet oDs = null;
MySqlCommand selectcommand = null;
MySqlCommand insertcommand = null;
MySqlCommand updatecommand = null;
MySqlCommand deletecommand = null;
private DataTable dt = null;
private DataTable dtDet = null;
private String sSelProcName = null;
private String sInsProcName = null;
private String sDelProcName = null;
private String sUpdProcName = null;
private int voucType;public MySqlConnection oCn = new MySqlConnection();
MySqlTransaction oTrn = null;Following is the NewVoucher procedure which will setup VoucherForm to open with blank record:
public DataSet NewVoucher()
{
DataSet vDs = new DataSet();
oCn = da.GetConnection();if (oCn == null) { oCn.Open(); } try { DataTable dt = new DataTable(); //=============================================================================== //--- Set up the Select Command //===============================================================================
String sqlSelect = "Select vID, vTypeID, vNo, accCodeDR, accCodeCR, vDate, vChqNo, vChqDt, vPayName, vRemarks, vAmount from vMaster";
sqlDataMaster = new MySqlDataAdapter(sqlSelect, oCn);
sqlDataMaster.FillSchema(dt, SchemaType.Source);
vDs.Tables.Add(dt);
VoucherDetails vdet = new VoucherDetails();
DataTable dtDet = new DataTable();
dtDet = vdet.NewVoucherDet();vDs.Tables.Add(dtDet);
vDs.Tables[0].Columns["vID"].AutoIncrement = true;
vDs.Tables[0].Columns["vID"].AutoIncrementSeed = -1;
vDs.Tables[0].Columns["vID"].AutoIncrementStep = -1;
vDs.Tables[1].Columns["vID"].AutoIncrement = true;
vDs.Tables[1].Columns["vID"].AutoIncrementSeed = -1;
vDs.Tables[1].Columns["vID"].AutoIncrementStep = -1;
vDs.EnforceConstraints = false;
vDs.Relations.Add("VouchersToVoucherDetails", vDs.Tables[0].Columns["vID"], vDs.Tables[1].Columns["vID"]);} catch (MySqlException e) {
-
Try this:
txtChqDt.DataBindings.Add(new Binding("Value", bs, "vChqDt"));
(Bind to the "Value" property instead of the "Text" property of txtChqDt).
-
Thanks for your reply, I will definitely try this.. Meanwhile I've noticed that when form load, and if user manually select date in datetimepicker, ONLY then date is saved!!!! Otherwise not.... Any thoughts???
I'm not clear on what / when "Otherwise not" refers to. All controls go through a series of events before they are fully initialized. If in some cases you see properties that are not available (and then are), you may be accessing them too soon; or not the right property in the current context (e.g. "Text" versus "Value"). The easiest thing to do is simply display the properties of interest at key points in the program which will give one a better understanding of the "life" of a control.
-
I'm not clear on what / when "Otherwise not" refers to. All controls go through a series of events before they are fully initialized. If in some cases you see properties that are not available (and then are), you may be accessing them too soon; or not the right property in the current context (e.g. "Text" versus "Value"). The easiest thing to do is simply display the properties of interest at key points in the program which will give one a better understanding of the "life" of a control.
-
Actually I am referring otherwise to when Form is load but user did not select any date in DateTimePicker and trying to saved the record with default Today's date.
-
Sorry for late reply, Yes I've tried the "Value"... As you've noticed from my code that it's Master/Detail application, and I've create a relation VouchersToVoucherDetails in form code. Before modification of "Value" to DataBindings of vChqDt, vID was showing -1 on form load. But now it is showing null and when try to save the records it say vID cannot be null, and when press ok then no record is save to database.
-
Hi, problem is solved, I've modified the "Text" and change to "Value", and also add fourth parameter "true" which enable formatting. After which all is going well. Thanks for your guidance and support. Really appreciate. Ahmed