There are 2 textboxs to accept from date and to date.

I will be comparing the fromdate value and todate with the table column.

If the record matches then only that row will get display in the datagridview.

Recommended Answers

All 19 Replies

Yes, you can use the 'BETWEEN' operator while executing the SQL Query.

This is how I am trying to write code
vate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

con.Open()

        cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA WHERE ida=textbox1.text,rda=textbox2.text", con)
        dr = cmd.ExecuteReader()
        If Not dr.Read() Then
            MsgBox("Invalid details")
        ElseIf 


    After **ElseIf** I am not able to write exact code.
    I want to show those particular rows in the gridview and that code should be shown in else part.

The statement that you are assinging to cmd won't take you anywhere.
1st of all you should change it to :

 cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA WHERE ida='" & textbox1.text & "' and rda='" & textbox2.text &"'", con)

And then you can add date criteria like this

     cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA WHERE ida='" & textbox1.text & "' and rda='" & textbox2.text &"' and date_field between '" & start_date & '" and '" & end_date & "'"

After else if u need to mention the condition and then continue with the code....if no condition is present then just use else

show the exact code what u have done after adam_k has post so we can help u more on it...

adam_k

cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA WHERE ida='" & textbox1.text & "' and rda='" & textbox2.text &"' and date_field between '" & start_date & '" and '" & end_date & "'"

here,
cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA WHERE ida='" & textbox1.text & "' and rda='" & textbox2.text &"' and date_field between '" & start_date & '" and '" & end_date & "'"

Why to show the date field between '" & start_date & '" and '" & end_date & "'"?
The ida and rda are the textboxes to accept the start date and end date itself.
I am actually not getting ur comment '" & start_date & '" and '" & end_date & "'"
And also after this condition how to show those records that match start date to end date in the gridview?

Sorry let me change it as both dates form one column only.
Is this right?

cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA where ida between='" & textbox1.text & "' and '" & textbox2.text & "'

yes it will be better if u have one column only for date so that u can aslo use the between case

poojavb
Ok thanku for reply .
But,Is this right?
cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA where ida between='" & textbox1.text & "' and '" & textbox2.text & "'")

cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA where ida between '" + textbox1.text + "' and '" + textbox2.text + "'")

after between '=' sign wont come....

poojavb
I have written code in this way. Accept from date and to date from text boxes then with the help of coding show the particular rows in gridview. The code I wrote is not so correct but I tried it: I have written code in button.

 con.Open()
        i = DataGridView1.CurrentCell.RowIndex
        cmd = New SqlCommand("SELECT COUNT(*) FROM IssueMBA WHERE ida between '" + TextBox1.Text + "'and  '" + TextBox2.Text + "'", con)
        dr = cmd.ExecuteReader()
        If Not dr.Read() Then
            MsgBox("Invalid details")
        ElseIf ((dr(0).ToString() >= TextBox1.Text) And (dr(0).ToString() <= TextBox2.Text)) Then
            DataGridView1.Rows(i).Cells(0).Value.ToString()
            DataGridView1.Rows(i).Cells(1).Value.ToString()
            DataGridView1.Rows(i).Cells(2).Value.ToString()
            DataGridView1.Rows(i).Cells(3).Value.ToString()
            DataGridView1.Rows(i).Cells(4).Value.ToString()
        End If




        con.Close()

Can you correct it?

Y are u validating the condition again....its the same thing that the query will give u...just show the datagrid with the result as I have shown....

ElseIf ((dr(0).ToString() >= TextBox1.Text) And (dr(0).ToString() <= TextBox2.Text)) Then

 Try
                Dim myCommand As New SqlCommand
                With myCommand
                    .CommandText = "select * IssueMBA WHERE ida between '" + TextBox1.Text + "'and  '" + TextBox2.Text + "'"
                   .CommandType = CommandType.Text
                    .Connection = Conn
                End With
                Dim dt As New DataTable
                dt.Load(myCommand.ExecuteReader)
                With DatagridView1
                    .AutoGenerateColumns = True
                    .DataSource = dt
                End With
            Catch ex As Exception
                Throw ex
            End Try

instead of writing select count(*) write the column names that u need in ur datagridview

poojavb
Yes I want to show all the columns with matching rows as per the condition.
So I used (*)

let's get back to step 1: Define your need. Are you trying to get a number showing how many records match your criteria (ie count(*)) or are you trying to get all records matching (ie select * from ... ).

In both cases criteria is the same, but the result is different.

adam_k
Currently what I am trying to do is :
1]Added 2 textboxes and a button and gridview on the form.
2] User will enter 2 dates in both text boxes. This 2 dates will be considered as frmdate and todate.
3]Then User will click on the button.
4]The button code should do:
Match this frmdate and todate to one of the columns called as "ida" in the database table IssueMBA.
So,from this column ,the rows that are between frmdate and todate should display that whole row in the gridview.
5] for eg: the user enters frmdate as 1/5/2012 and todate as 5/5/2012 then in the gridview the only rows that match this criteria should be displayed.
So, the whole row matching this criteria should be displayed.

poojavb
Thanku very much Pooja :) Your code worked out. Thanks a lot for help :)

Ur welcome.....vote me up :) and mark the thread as solved if it helped u....

Poojavb actually this code doesn show all rows

select * will show all the rows....if u r mentioning some column names then it wont show??

what query are u using??? and what rows do u want to return??? paste it....

"select * from StudCIssue WHERE ida between '" + TextBox1.Text + "'and '" + TextBox2.Text + "'"

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.