Search Datagriew Problem

xswzaq

Well-known member
Joined
Jul 9, 2007
Messages
61
Programming Experience
Beginner
VB.NET:
Try
            CCNameSearchTable.Show()
            'Search by Ware House Only When Text Not Equal to "ALL"
            If Me.CCNSwarehouseComboBox.Text IsNot "ALL" And Me.CCNSindivBinComboBox.SelectedIndex = -1 Then
                CCNameSearchTable.CC_NAME_SEARCHTableAdapter.FillByWareHouseOnly(CCNameSearchTable.PtmnDataSet.CC_NAME_SEARCH, Me.CCNSwarehouseComboBox.Text)

end if
 Catch ex As Exception
            MessageBox.Show("Failed! " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

 Try
            CCNameSearchTable.Show()
            'Search by WareHouse (When warehouse text is not "ALL") and Date
            If Me.CCNSwarehouseComboBox.Text IsNot "ALL" And Me.CCNSindivBinComboBox.SelectedIndex = -1 And Me.CCNSFrom_DateTxtBox.Text IsNot String.Empty And Me.CCNSTo_DateTxtBox.Text IsNot String.Empty Then
                CCNameSearchTable.CC_NAME_SEARCHTableAdapter.FillByWareHouseDate(CCNameSearchTable.PtmnDataSet.CC_NAME_SEARCH, Me.CCNSwarehouseComboBox.Text, CType(Me.CCNSFrom_DateTxtBox.Text, Date), CType(CCNSTo_DateTxtBox.Text, Date))

 End If
        Catch ex As Exception
            MessageBox.Show("Failed! " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

I have this code to handle my search, but the problem is whenever I click, It always said that error, cann't convert "" to Date?
 
First of all, this:
VB.NET:
Me.CCNSwarehouseComboBox.Text IsNot "ALL"
is wrong. The IsNot operator tests whether two references refer to the same object. Given that one of the objects is a string literal that you have created on the spot, that can not possibly evaluate to False. It should be:
VB.NET:
Me.CCNSwarehouseComboBox.Text <> "ALL"
That will compare the two strings by characters, which is what you want.

That exception is being thrown because of this part:
VB.NET:
CType(Me.CCNSFrom_DateTxtBox.Text, Date), CType(CCNSTo_DateTxtBox.Text, Date)
As the error message says, you can't convert an empty string to a Date. If your TextBoxes are empty then how can you convert their contents to Dates? If you want the user to input a date then you should be using a DateTimePicker, not a TextBox. Don't hit nails in with a spanner.
 
Don't hit nails in with a spanner.

lmao.. i love it

@OP:
In times like these, i write the query so it does the choosing:

SELECT * FROM wareHouse WHERE itemDate = @itemDate OR @itemDate IS NULL

If i supply a date, i get items from the date. If I supply a null, i get all items. DatePickers cant show nulls, but you can use alternative components or use a checkbox to determine whether to refine by date or not
 
In my sql, the program does not accept "IS NULL". As for datetimepicker, it is automaticly pick today date. So nomatter, there will always be a date for from_date and to_date textbox. But in my program, users have a choice whether that they want to input date or not.
 
In my sql, the program does not accept "IS NULL". As for datetimepicker, it is automaticly pick today date. So nomatter, there will always be a date for from_date and to_date textbox. But in my program, users have a choice whether that they want to input date or not.

IS NULL is a feature of SQL, not your program. DateTImePickers cannot be null and this is a much discussed topic on the net. Some user controls rectify this, or you can use a checkbox to choose whether to send a null or the date in..
 
I got it to work now, I put the search with Date in one sub, and without date in another sub. And write code, if the date txtbox is blank call without date sub, else called with date
 
Back
Top