multiple parameter selection in crystal report in .net not working - vb.net

Form contains 1 texbox to hold Student ID and two Date-time Picker to select fees paid month by student and i am able to select date range of all students , where unable to filter fromdate to Todate with particular one Student.
i am getting error ....Please Help....... here is my problem code
Dim dbCommand As New OleDb.OleDbCommand
Dim myreport As New CrystalReport2
Dim Dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
dbCommand.CommandText = "Select FeesMonth between #" & FromDate.Text & "# and #" & ToDate.Text & "# From StudentPayments Where StudentID= '" & TextBox1.Text & "'"
da.SelectCommand = dbCommand
da.Fill(Dt)
myreport.SetDataSource(Dt)
CrystalReportViewer1.ReportSource = myreport
CrystalReportViewer1.Show

you have to give Connectionstring to dbCommand.
like -
OleDbConnection con=new OleDbConnection ();
dbCommand.Connectionstring=con;

Related

SQL filter Date from Label in VB.net

I am stuck with little problem. I have a sql database with DATE column. It is populated from a Label like this: Label1.text = Date.today
and need to show records from one date in datagridwiev. So I need filter date using date from Label. So far I have this:
Public Sub ShowData()
cmd = New SqlCommand("Select * FROM Cisnik WHERE Datum = #" & Label3.Text & "# ", con)
If con.State = ConnectionState.Closed Then con.Open()
myDA = New SqlDataAdapter(cmd)
myDataSet = New DataSet()
myDA.Fill(myDataSet, "Cisnik")
DGV3.DataSource = myDataSet.Tables("Cisnik").DefaultView
End Sub
This code throws : Incorrect syntax near '11.'. The number 11 is a part of European form of date 24.12.2018
The database works OK. Only need this filter problem to solve.
Try:
cmd = New SqlCommand("Select * FROM Cisnik WHERE Datum = '" & cdate(label3.Text).ToString("yyyy-MM-dd") & "'", con)
Your query will be:
Select * FROM Cisnik WHERE Datum = '2018-11-11'
(date example)

i want to filter my data with this from using visual studio 2015

I want to sort the data in the database with the date as the main condition with 2 date time picker 1 as the starting date and the other as the limit with this code by using between but I do not know the correct query form...my from looks like this the first DTP name is DTPDari and second DTPSampai
Call KONEKSI()
CMD = New OleDbCommand("SELECT * FROM Pembayaran where tanggal_pembayaran BEETWEEN '" & DTPDari.Value & "'AND tanggal_pembayaran = '" & DTPSampai.Value & "'", CONN)
DR = CMD.ExecuteReader
DR.Read()`
From the little what I understand from your question you can use any of the below
(syntax not tested)
SELECT * FROM Pembayaran where tanggal_pembayaran
WHERE (tanggal_pembayaran BETWEEN '" & DTPDari.Value & "' AND '" & DTPSampai.Value & "')
or
SELECT * FROM Pembayaran where tanggal_pembayaran
WHERE (tanggal_pembayaran > '" & DTPDari.Value & "') and (tanggal_pembayaran < '" & DTPSampai.Value & "')
Adding Function sample asper your request
Sub GetDetails()
Dim connectionString As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString.ToString()
Dim connection As New SqlConnection(connectionString)
Dim queryString2 = "SELECT *
FROM dbo.Customers
WHERE (CreationDate BETWEEN #param1 AND #param2)"
Dim cmd As SqlCommand = New SqlCommand()
cmd.CommandText = queryString2
cmd.Connection = connection
cmd.Parameters.AddWithValue("#Param1", from_DateTimePicker.Value.Date)
cmd.Parameters.AddWithValue("#param2", to_DateTimePicker.Value.Date)
connection.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Console.WriteLine("{0}", reader(0))
'here fill on datatable Or anything you want
End While
connection.Close()
End Sub

vb.net getting value from a new name sql database

Dim cd As SqlCommand = New SqlCommand("select datepart(mm,Birthday) as MonthDates from Information where Name='" & cbname.Text & "'", con)
Dim reader1 As SqlDataReader = cd.ExecuteReader
While reader1.Read
Dim mon As Integer = reader1("MonthDates")
lblbday.Text = mon
I used this code to assume that the new named column in SQL will be read in vb net but its not reading the new named Column "MonthDates".
What is wrong with the code? Please help. Thank you in advance!
You should dispose connection after query (in my example USING do this):
Using con As New SqlConnection(....)
Dim cd As SqlCommand = New SqlCommand("select datepart(mm,Birthday) as MonthDates from Information where Name='" & cbname.Text & "'", con)
Dim mon As Integer = cd.ExecuteScalar()
lblbday.Text = mon
End Using

Adding DateTime as a NEW column on my database

I'm getting syntax error on the part of ALTER TABLE etc. please help thanks in advance :) Visual Basic
Dim cnn As New OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\Ashe\Documents\Visual Studio 2010\Projects\WindowsApplication1\Guidance System.accdb")
Dim mydate As DateTime
mydate = Me.DateTimePicker1.Value
Dim AddCol = "ALTER TABLE Attendance " & _
"ADD '" & mydate & "');"
Using cmd = New OleDbCommand(AddCol, cnn)
cnn.Open()
cmd.ExecuteNonQuery()
End Using
I come up with this code and it's now working. thanks y'all! :)
Dim cnn As New OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\Ashe\Documents\Visual Studio 2010\Projects\WindowsApplication1\Guidance System.accdb")
Dim mydate As DateTime
mydate = DateTimePicker1.Value
mydate.ToString("yyyy-MM-dd")
Dim AddCol = "ALTER TABLE Attendance " & _
"ADD '" & mydate & "' varchar(100);"
Using cmd = New OleDbCommand(AddCol, cnn)
cnn.Open()
cmd.ExecuteNonQuery()
End Using
You haven't closed a string
Dim cnn As New OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\Ashe\Documents\Visual Studio 2010\Projects\WindowsApplication1\Guidance System.accdb")
Dim mydate As DateTime
mydate = Me.DateTimePicker1.Value
Dim AddCol = "ALTER TABLE Attendance " & _
"ADD '" & mydate & "' varchar(100);"
Using cmd = New OleDbCommand(AddCol, cnn)
cnn.Open()
cmd.ExecuteNonQuery()
End Using
Note: Added varchar(100) after the last comment was posted to have a working code here too.

Search between two dates in access database using sql

This is my code for search in access database 2010. My problem is that when I search between two datetimepicker the result is wrong in datagridview, I mean when I search from specific records between May and June it shows me records also from February.
Private Sub Search_Record()
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim sSQL As String = String.Empty
Dim bookdetials As New frmContactDetails
Try
'get connection string declared in the Module1.vb and assing it to conn variable
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "SELECT contact_id, first_name , birth_date, book_num, send_from, no_answer, no_answer_by, rec, book_answer_name, book_answer_num, send_date, send_to, project_name FROM tblAddressBook"
If CheckBox1.Checked = True Then
sSQL = sSQL & " where project_name like '%" & Me.TextBox2.Text & "%' " & _
" AND birth_date between '" & DateTimePicker1.Text & "' AND '" & DateTimePicker2.Text & "'"
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
Me.dtgResult.DataSource = dt
Label4.Text = dt.Rows.Count
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
datepicker text should be converted to datetime format in sql
I had the same problem, the solution was too silly but it worked
use text instead of datetime in the db
make sure the datetimepicker enters "short format" data