Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
Private Sub viewrptrs_Click(sender As System.Object, e As System.EventArgs) Handles viewrptrs.Click
       Dim str As String
       Dim datefrom = DateTimePicker1.Value.Date
       Dim endfrom = DateTimePicker2.Value.Date
       Dim con As New SqlConnection("Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=Art Station Management System;Integrated Security=True")
       str = "Select sstudentid, sname, smiddlename, ssurname, ccoursename from StudentRegister WHERE sdatereg BETWEEN " & datefrom & " AND " & endfrom & ""
       Dim com As New SqlCommand(str, con)
       Dim da As New SqlDataAdapter(com)
       Dim ds As New DataSet
       da.Fill(ds, "StudentRegister") 'ERROR ON THIS LINE
       DataGridView1.DataSource = ds.Tables(0)
       da.Dispose()
   End Sub
Posted
Updated 15-Jan-16 1:31am
v2
Comments
Richard Deeming 15-Jan-16 10:41am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Ashutosh Dutondkar 15-Jan-16 12:41pm    
Your solution worked for me. Very much thankful to you, i was struggling with this from 2 days which help me out today. Also your code taught me about SQL Injection Attacks, thank you sou much! :)
Ashutosh Dutondkar 15-Jan-16 12:42pm    
Also it taught many things to me. :)

1 solution

Fixing the SQL Injection[^] vulnerability in your code will most likely solve the error message as well:
VB.NET
Private Sub viewrptrs_Click(sender As System.Object, e As System.EventArgs) Handles viewrptrs.Click
    Using con As New SqlConnection("Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=Art Station Management System;Integrated Security=True")
        Using com As New SqlCommand("Select sstudentid, sname, smiddlename, ssurname, ccoursename from StudentRegister WHERE sdatereg BETWEEN @datefrom AND @dateto", con)
            com.Parameters.AddWithValue("@datefrom", DateTimePicker1.Value.Date)
            com.Parameters.AddWithValue("@dateto", DateTimePicker2.Value.Date)
            
            Using da As New SqlDataAdapter(com)
                Dim ds As New DataSet()
                da.Fill(ds, "StudentRegister")
                DataGridView1.DataSource = ds.Tables(0)
            End Using
        End Using
    End Using
End Sub

While you're at it, do yourself a favour and give your controls meaningful names. You might remember that TextBox42 holds the "frood name" today; but when you come back to your code in six months, you'll be struggling.
 
Share this answer
 

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