hi every 1/ . i am having problem in sql query. if i double click on the dataset. i am not sure which statement to write to restrict the user so that he/she cannot enter the date in the past. i have droped the data set on the form and my data entry is through datagrid view.kindly help.

Its a desktop app and its a booking form. if a customer comes to book a room . some time it happens by mistake we enter wrong year or previous dates for future booking. i want to show an error msg if user enters past date.

ok lets assume you have a form , have so many controls , and a datetimepicker , now you want to save records of the current date , and restrict user to save records of past date , then you can do like this .

if datetimepicker.value.date.tostring() < now.date.tostring() then 

msgbox("you are trying to save records of past date.")
else
'allow user
end if

Regards

i am inserting data through data gridview so where shall i write ths code.

is there any column of date ?

yes there is a column of date in data gridview.which is a data field in sql . i enter booking date through datagridview..so plz tell me wats the solution

well bro do this

if datagrid.item(columnindex,rowindex).value.tostring() < now.date.tostring() then 

msgbox("you are trying to save records of past date.")
else
'allow user
end if

you are using loop to insert your data ? if you find any prob then please post your insertion code here.

Best Regards

thnx for the reply bro. well wat i have done is i just added data source and i dragged the dataset on to the form so it automatically generated the binding source and table adapter ..so i can straight away enter the data into the gridview an it is being saved into the data based.there is no code as such wriiten by my self.

hmmm , well i dont know what you have done so far but here is a sample code , try to make is workable for you ,
consider we have values in grid , we have two columns , userid , date , now do this

dim i as integer
for i = 0 to datagrid.rows.count -1 
if datagrid.item(1,i).value.tostring() < now.date.tostring() then 

msgbox("you are trying to save records of past date.")
else
'allow user
end if
next

in else section you can code your insertion coding , hope this will helps you if no then please write some code , if you got any error then we are here to solve them :)

Regards

to restrict the user so that he/she cannot enter the date in the past.

I have another preposition: do now allow user to write date. Use MonthCalendar to select the date, and do not show the date older then you want (or today`s date or what ever).
Can you do that?

hi i used this code

Dim i As Integer
For i = 0 To NewBookingDataGridView.Rows.Count - 1
    If NewBookingDataGridView.Item(1, i).Value.ToString() < Now.Date.ToString() Then
        MsgBox("you are trying to save records of past date.")
    Else
        Me.Validate()
        Me.CustomerBindingSource.EndEdit()

        Me.BookingBindingSource.EndEdit()

        If Me.save Then
            MsgBox("Your Records Were Saved")
        End If
    End If
Next

when i run the project and if i insert a past date it give me correct error msg but i have to click the ok button three times and after that i am getting an error msg. null exception can be handled at this line...If NewBookingDataGridView.Item(1, i).Value.ToString() < Now.Date.ToString() Then

i would like to mention 1 more thing here, date is my column number 4 in my datagridview .could you please tell me how would i make the changes to the above code to make the code working.

You should use a cellValidating event. It occurs before Leaving the cell. Its strictly meant for validating the cell value.
This event will notify user as soon as he will try to confirm the inserted value into a cell (as said, just before leaving cell).
And do not use a loop through rows, use only selected row!

private void NewBookingDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    //validating only for 2nd column:
    if(e.ColumnIndex == 1)
    {
         DateTime input = Convert.ToDateTime(e.FormatedValue.ToString()); 
         if(input < DateTime.Now.Date)
         {
              e.Cancel = true;
              MessageBox.Show("You are trying to save records of past date.");
         }
    } 
}

So this is in my opinion the best way, no need any loops or any other checking. Best to notify the user as soon as he does something wrong. And this why we have CellValidating event.

Hope it helps,
bye

still cannot solve this problem can any one help me please with this.i am using this code to restrict user from not entering past date in the datagridview. date is my column number 4 in the datagridview could any one tell me how to do this..

If NewBookingDataGridView.Item(3, 3).Value.ToString() < Now.Date.ToString() Then
MsgBox("you are trying to save records of past date.")
Else
Me.Validate()
Me.CustomerBindingSource.EndEdit()

        Me.BookingBindingSource.EndEdit()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.