|

February 12th, 2005, 10:12 AM
|
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here is my coding:
Quote:
|
quote:// Web control declaration
|
Quote:
protected System.Web.UI.WebControls.TextBox txtDate1;
protected System.Web.UI.WebControls.TextBox txtDate3;
protected System.Web.UI.WebControls.TextBox txtDate2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox txtRefNum;
protected System.Web.UI.WebControls.Calendar Calendar1;
// Nothing in my Page Load function
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
// Date string is inserted into textbox after date selection from calendar control
private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
bool DateIsInserted = false;
if(txtDate1.Text == "" && DateIsInserted == false)
{
txtDate1.Text = Calendar1.SelectedDate.ToString("d/M/yyyy");
DateIsInserted = true;
}
if(txtDate2.Text == "" && DateIsInserted == false)
{
txtDate2.Text = Calendar1.SelectedDate.ToString("d/M/yyyy");
DateIsInserted = true;
}
if(txtDate3.Text == "" && DateIsInserted == false)
{
txtDate3.Text = Calendar1.SelectedDate.ToString("d/M/yyyy");
DateIsInserted = true;
}
}
// Update to database with button click
private void UpdateDataToDB()
{
string connectionString="server=(local);database=myDB;" +
"integrated security=true;Trusted_Connection=Yes;";
SqlConnection DbConnection = new SqlConnection(connectionString);
SqlTransaction Tran;
DbConnection.Open();
Tran = DbConnection.BeginTransaction();
try
{
string queryString = "INSERT INTO Payment(RefNum, Date1, " +
"Date2, Date3)" +
"Values(@ReservationRefNum, @Date1, @Date2, @Date3)";
SqlCommand CommandString = new SqlCommand(queryString, DbConnection);
CommandString.Transaction = Tran;
SqlParameter RefNumParam = new SqlParameter("@RefNum", SqlDbType.VarChar, 50);
RefNumParam.Value = txtRefNum.Text;
CommandString.Parameters.Add(RefNumParam);
SqlParameter Date1Param = new SqlParameter("@Date1", SqlDbType.DateTime);
Date1Param.Value = txtDate1.Text;
CommandString.Parameters.Add(Date1Param);
SqlParameter Date2Param = new SqlParameter("@Date2", SqlDbType.DateTime);
Date2Param.Value = txtDate2.Text;
CommandString.Parameters.Add(Date2Param);
SqlParameter Date3Param = new SqlParameter("@Date3", SqlDbType.DateTime);
Date3Param.Value = txtDate3.Text;
CommandString.Parameters.Add(Date3Param);
CommandString.ExecuteNonQuery();
Tran.Commit();
Response.Write("Success!");
}
catch
{
Tran.Rollback();
}
finally
{
DbConnection.Close();
}
}
|
If you select the date using from the calendar control, you may find that you can't update to database. However, it is not the case if we key in text using keyboard.
|