Click here to Skip to main content
15,891,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am trying to save the selected date in the database.
I did that with the help of date picker, now I need to save it in the database.

How to do that?
How to get the selected date and store it in the database?

Can any one help to me?


this is my code
C#
DatePickerDialog.OnDateSetListener startdate=new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            dateAndTime.set(Calendar.YEAR, year);
            dateAndTime.set(Calendar.MONTH, monthOfYear);
            dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        }
    };
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
            dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            dateAndTime.set(Calendar.MINUTE, minute);
            view.setCurrentHour(Calendar.HOUR_OF_DAY);

        }
    };

///////////////
           super.onStart();
		dbHelper=new DatabaseActivity(this);
	        
	    	Button startbtn=(Button)findViewById(R.id.AddStartButton);
	    	startbtn.setOnClickListener(new View.OnClickListener() { 
				public void onClick(View v) { 
					new DatePickerDialog(AddTask.this,startdate, 
							dateAndTime.get(Calendar.YEAR), 
							dateAndTime.get(Calendar.MONTH), 
							dateAndTime.get(Calendar.DAY_OF_MONTH)).show(); 			
					} 
		 
	    	btn=(Button)findViewById(R.id.time);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new TimePickerDialog(Alarm.this,t,
                        dateAndTime.get(Calendar.HOUR_OF_DAY),
                        dateAndTime.get(Calendar.MINUTE),
                        true).show();
                }
        });

	    	
	    	private DatePickerDialog.OnDateSetListener startdate = new DatePickerDialog.OnDateSetListener() {
	    		    		
	    		// onDateSet method
	    		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
	    		String date_selected = String.valueOf(monthOfYear+1)+" /"+String.valueOf(dayOfMonth)+" /"+String.valueOf(year);
	    		Toast.makeText(AddTask.this, "Selected start Date is "+date_selected, Toast.LENGTH_SHORT).show();
	    			
	    		
	    		//Task date=new Task(date);
	    		//dbHelper.AddTask(date);
	    		
	    		}
	    		};   	
	    	
	   });	   


by this way am getting and toast the selected date


Thank you friends
Posted
Updated 20-Apr-11 0:34am
v4
Comments
Dalek Dave 20-Apr-11 3:32am    
Edited for Grammar and Readability.
CookieMan90 1-May-12 9:26am    
Hi, I was wondering, but how do you retrieve and display information from a database using DatePicker and TimePicker?

select date from datetime picker is easy:

DateTime dateTime = datetimepicker.Value;


inserting this value in a SQL database can be done with SQLParameters:

_sqlConnection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(@"INSERT INTO Tablename(columnDateTimeName) VALUES (@DateTimeParam)",_sqlConnection);
cmd.Parameters.Add("@DateTimeParam", SqlDbType.DateTime).Value = dateTime;
cmd.ExecuteNonQuery();


Hope it helps you!
 
Share this answer
 
v2
Comments
ezhilsigamani 20-Apr-11 6:00am    
thank you fri... check my updated question by this way am using the datepicker ....
Wannes Geysen 20-Apr-11 6:20am    
I don't get it. You aren't working with a dateTimePicker, but with a DatePickerDialog?
ezhilsigamani 20-Apr-11 6:37am    
@wannes i need both datetime but i did't mention that thats it sorry
How to do that?
Just like you save other data in database. Nothing different or new.

How to get the selected date
from the datepicker directly? No idea why or whats stopping you here.

Can any one help to me?
Looking for something else other than above? If Yes -> post what you are trying and are stuck, we will try to help.
 
Share this answer
 
Comments
ezhilsigamani 20-Apr-11 5:58am    
Thank you for your reply fri...i update my code pls check the code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900