3 Replies - 4601 Views - Last Post: 30 August 2012 - 11:00 AM Rate Topic: -----

#1 siddu1281  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 12

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

Posted 29 August 2012 - 10:19 PM

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);

                }

                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!");

        }  


Is This A Good Question/Topic? 0
  • +

Replies To: how to store date of ajax calender extender from asp.net to sql server

#2 siddu1281  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: how to store date of ajax calender extender from asp.net to sql server

Posted 30 August 2012 - 01:57 AM

guys i solved the problem here it is

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);

thank you
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: how to store date of ajax calender extender from asp.net to sql server

Posted 30 August 2012 - 05:48 AM

Since you have solved the problem, just wanted to throw this out. You can take this code...

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);

}



and change it to this...

cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.Parameters.AddWithValue("@Startdate", Startdate);



You should also use a using block for the SqlConnection and SqlCommand objects.

try
{
	using (SqlConnection con = new SqlConnection(connectionString)
	using (SqlCommand cmd = con.CreateCommand())
	{
		cmd.CommandText = "INSERT INTO first (Username, Password, Startdate) VALUES" + "(@Username,@Password,Startdate)";
		cmd.CommandType = CommandType.Text;
		
		cmd.Parameters.AddWithValue("@Username", Username);
		cmd.Parameters.AddWithValue("@Password", Password);
		cmd.Parameters.AddWithValue("@Startdate", Startdate);
		
		con.Open();
		
		cmd.ExecuteNonQuery();
	}
}
catch
{
	throw;
}



I decided to re-write your 66 lines of code and whitespace into 20 lines. The using block will automatically call Close and Dispose on the SqlConnection and SqlCommand classes. In the catch, you never want to throw a new exception because you lose the Stack Trace of the original exception. Simply throw the exception.
Was This Post Helpful? 1
  • +
  • -

#4 siddu1281  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: how to store date of ajax calender extender from asp.net to sql server

Posted 30 August 2012 - 11:00 AM

hello Ryan,

iam glad that you replied for my question. and you program is good i will surely implement.it also helped me to learn how to write the program precisely.

thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1