how to store date of ajax calender extender from asp.net to sql server [Resolved]

Posted by Siddu1281 under ASP.NET AJAX on 8/30/2012 | Points: 10 | Views : 4372 | Status : [Member] | Replies : 4
hiii

how to store the date of ajax calender extender from textbox to database(sql server 2005).

how to pass the date inthe following statement
ExecuteInsert(TextBox1.Text, TextBox2.Text, date);



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

public partial class test4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
private void ExecuteInsert(string Username, string Password, DateTime Startdate)
{
string connectionString = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString();
SqlConnection con = new SqlConnection(connectionString);
string sql = "INSERT INTO first (Username, Password, Startdate) VALUES" + "(@Username,@Password,Startdate)";

try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter("@Username", SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@Startdate", SqlDbType.DateTime);

param[0].Value = Username;
param[1].Value = Password;
param[2].Value = Startdate;

for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}

}

protected void Button1_Click(object sender, EventArgs e)
{
// DateTime date = DateTime.ParseExact(TextBox3.Text.ToString(),"",null);
// DateTime date = Convert.ToDateTime(TextBox3_CalendarExtender.SelectedDate);
ExecuteInsert(TextBox1.Text, TextBox2.Text, date);
Response.Write("Record was successfully added!");
}





Responses

Posted by: Shivu on: 8/30/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved

Parse the textbox string into datetime, using below line and pass it to SQL server. If your date format is change from below thing, change it accordingly.
DateTime Startdate= DateTime.ParseExact(txtfromdate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

Hope it helps


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

Posted by: Shivu on: 8/30/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Siddu,

Let me know format of date you are taking from ajax calender. If it is MM/dd/yyyy then, change the parsing code as below,

DateTime Startdate= DateTime.ParseExact(txtfromdate.Text, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);




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

Posted by: Siddu1281 on: 8/30/2012 [Member] Starter | Points: 25

Up
0
Down
hello shivu

iam getting an error as following
String was not recognized as a valid DateTime.
thank you for replying.

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

Posted by: Siddu1281 on: 8/30/2012 [Member] Starter | Points: 25

Up
0
Down
Thank you shiv. your answer helped me alot.


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

Login to post response