Failed to emit module BC36970 Visual Basic AND VB.NET - vb.net

I have method to fill DataGridView from SQL server database between tow dates my date column date in database is Date in this format "dd/MM/yyyy h:mm:ss tt", and this is the query:
Const sql As String = "SELECT * FROM money_sent WHERE date BETWEEN '2016-08-01 04:01:59.000' AND '2016-09-10 04:02:05.000'"
until now everything perfect , when I tried to use datetimepicker I get 2 errors:
"Constant expression is required"(for picker 1)
"Constant expression is required"(for picker 2)
Const sql As String = "SELECT * FROM money_sent WHERE date BETWEEN '" & DateTimePicker1.Value & "' AND '" & DateTimePicker1.Value & "'"
After this I tried to do this:
Dim date1 As DateTime = DateTimePicker1.Value
Dim date2 As DateTime = DateTimePicker2.Value
Const sql As String = "SELECT * FROM money_sent WHERE date BETWEEN '" & date1 & "' AND '" & date2 & "'"
Now I getting this error:
Failed to emit module'WindowsApplication.exe' (BC36970 Visual Basic AND VB.NET Failed to emit module.)
What I can do in this situation please help
This is the method code :
If Conn.State = ConnectionState.Open Then
Conn.Close()
End If
Conn.Open()
Dim date1 As DateTime = DateTimePicker1.Value
Dim date2 As DateTime = DateTimePicker2.Value
Const sql As String = "SELECT * FROM money_sent WHERE date BETWEEN '" & date1 & "' AND '" & date2 & "'"
Dim adt As New SqlDataAdapter(sql, Conn)
Dim dt As New DataTable
adt.Fill(dt)
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
MetroGrid2.Rows.Add()
MetroGrid2.Rows(i).Cells(1).Value = dt.Rows(i).ItemArray(1)
Dim cell As DataGridViewComboBoxCell = DirectCast(MetroGrid2.Rows(i).Cells(2), DataGridViewComboBoxCell)
cell.Value = dt.Rows(i).ItemArray(2)
MetroGrid2.Rows(i).Cells(3).Value = dt.Rows(i).ItemArray(3)
MetroGrid2.Rows(i).Cells(4).Value = dt.Rows(i).ItemArray(4)
'DataGridView1.Rows(i).Cells(4).Value = "DELETE"
MetroGrid2.Rows(i).Cells(5).Value = dt.Rows(i).ItemArray(5)
MetroGrid2.Rows(i).Cells(6).Value = dt.Rows(i).ItemArray(6)
MetroGrid2.Rows(i).Cells(7).Value = dt.Rows(i).ItemArray(7)
MetroGrid2.Rows(i).Cells(8).Value = dt.Rows(i).ItemArray(8)
MetroGrid2.Rows(i).Cells(9).Value = dt.Rows(i).ItemArray(9)
MetroGrid2.Rows(i).Cells(10).Value = dt.Rows(i).ItemArray(10)
MetroGrid2.Rows(i).Cells(12).Value = dt.Rows(i).ItemArray(12)
MetroGrid2.Rows(i).Cells(13).Value = dt.Rows(i).ItemArray(0)
Next
Conn.Close()

Related

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 - where column_name and column_name2 between column_name2 (date) and column_name2 (date)

So, I tried to get the UID.Text and try to show timein.
I want try to do this where uid.text = uid timein >= yyyy-MM-dd and timein <= yyyy-MM-dd
See the Image below, so that you'll see the code and error.
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "Server=localhost; uid=xx; pwd=xx; database=studentdatabase; Convert Zero Datetime=True;"
Dim Adapter As New MySqlDataAdapter
Dim BSource As New BindingSource
Dim DT As New DataTable
Try
MySqlConn.Open()
Dim Query As String
Query = "Select TimeIN, TimeOUT from tbl_time where UID =" & TextBox_UID.Text & " between TimeIn = '" & DateTimePicker2.Text & "' And TimeIn ='" & DateTimePicker1.Text & "'"
MSCommand = New MySqlCommand(Query, MySqlConn)
Adapter.SelectCommand = MSCommand
Adapter.Fill(DT)
BSource.DataSource = (DT)
DataGridView_Search.DataSource = BSource
Adapter.Update(DT)
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
The correct SQL syntax for your query would be this:
select * from tbl_time where uid=uid and timein between date1 and date2
Try using this code instead:
Query = "Select TimeIN, TimeOUT from tbl_time where UID =" & TextBox_UID.Text & " TimeIn between '" & DateTimePicker2.Text & "' And '" & DateTimePicker1.Text & "'"

How to handle null DateTimes in .NET and convert them to specified DateTime?

In the last line, it will give an error "Year, Month, and Day parameters describe an un-representable DateTime." when dr.item("t$ltdt") is equal to null.
How can I handle null DateTimes and convert them into specified DateTime?
Dim sqlCur As String = "SELECT * FROM baan.ttiitm001800 WHERE (t$kitm ='1' or t$kitm='4') " & ItemCondtion & " ORDER BY t$item"
Dim cmd As New OracleCommand(sqlCur, connBaan)
Dim dr As OracleDataReader = cmd.ExecuteReader()
If dr.HasRows Then
While dr.Read()
Dim ChkItmCd As String = "SELECT Item_Code FROM MPC_Item_Master WHERE Item_Code='" & Trim(dr.Item("t$item")) & "' "
db.OpenTable(ChkItmCd)
If db.Eof Then
ItmType = Trim(dr.Item("t$kitm"))
If (Trim(dr.Item("t$kitm")) = "1") Then
ItmType = "PURCHASED"
ElseIf (Trim(dr.Item("t$kitm")) = "4") Then
ItmType = "COST"
End If
Dim ltdt As Date = dr.Item("t$ltdt")
You probably want to use the Convert.ToDateTime method. It takes in a large variety of formats and converts it to a DateTime.
Also, make sure your input is not null:
Dim ltdt As Date
If dr.Item("t$ltdt") IsNot Nothing Then
ltdt = Convert.ToDateTime(dr.Item("t$ltdt"))
End If

why my basic date picker couldn't convert the input into the datetime format ?

I'm using basic date picker for vb.net and oracle as my database. when i insert the date from basic date picker, i got this error >> ORA-01797: this operator must be followed by ANY or ALL
this is my code :
Private Function GetDate(ByVal bdp1 As Date) As DataSet
Dim connectionString As String = "Data Source = ***; User ID =***; Password =**;"
Dim sqlConnection As OracleClient.OracleConnection = New OracleClient.OracleConnection(connectionString)
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= ('" & bdp1 & "' , 'MM/DD/YYYY') and tran_dttm > ('" & bdp1 & "', 'MM/DD/YYYY')"
Dim sqlCommand As OracleClient.OracleCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
sqlCommand.CommandTimeout = 0
Dim dataAdapter As OracleClient.OracleDataAdapter = New OracleClient.OracleDataAdapter(sqlCommand)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
Try, instead:
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= :dtm1 and tran_dttm > :dtm2"
Dim sqlCommand As OracleClient.OracleCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
sqlCommand.Parameters.AddWithValue("dtm1",bdp1)
sqlCommand.Parameters.AddWithValue("dtm2",bdp1)
Which a) avoids the possibility of SQL injection, and b) Keeps the date as a date throughout, rather than mangling it to/from a string.
It doesn't fix the logical issue with your query though - where you're trying to find a row where tran_dttm is both "less than or equal" and "greater than" the same value.
Try this ..
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= cdate('" & bdp1.Value.ToString & "') and tran_dttm > cdate('" & bdp1.Value.ToString & "')"
Try this:
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= '" & bdp1.ToString("MM/dd/yyyy") & "' and tran_dttm > '" & bdp1.ToString("MM/dd/yyyy") & "'"
But for oracle the default datetime format is YYYY-MM-DD. You have three options:
Change the default file format for the one that suits your desires
Use the default format: bdp1.ToString("yyyy-MM-dd")
Use the todate oracle function specifiying the read format:
" to_date('" & bdp1.ToString("MM/dd/yyyy") & "', 'mm/dd/yyyy') "

how oracle read date from basic date picker for vb.net?

hello I'm using oracle & vb.net
this is my code for select statement and I want to display the selected date from interface which i use Basic Date Picker and i assign that as bdp1
Dim queryString As String = "select * from abc where (tran_dttm <= to_date( '" & bdp1 & "' ,'MM/DD/YYYY') and tran_dttm > to_date( '" & bdp1 & "' ,'MM/DD/YYYY')and lpt = '" & ListBox1 & "' and device = '" & strMaterial & "')"
select * from abc where trunc (tran_dttm) = to_date('" & bdp1 & "','MM/DD/YYYY')
Parameters should be prefixed with the : and should be included as text directly in the command. It is the job of the framework to pass them with their values to the database engine
Private Function GetDate(ByVal strMaterial As String, ByVal ListBox1 As String, ByVal bdp1 As Date) As DataSet
Dim connectionString As String = "Data Source = ***; User ID = ***; Password = **;"
Dim queryString As String = "BEGIN select * from abc " & _
"where (tran_dttm <= to_date(:bdp1,'MM/DD/YYYY') and " & _
"tran_dttm > to_date(:bdp1 ,'MM/DD/YYYY') and " & _
"lpt = :lb1 and device = :mat); END;"
Using sqlConnection = New OracleClient.OracleConnection(connectionString)
Using sqlCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
sqlCommand.CommandTimeout = 0
sqlCommand.Parameters.Add(New OracleParameter(":bdp1", OracleType.DateTime)).Value = bdp1
sqlCommand.Parameters.Add(New OracleParameter(":lb1", OracleType.VarChar)).Value = lstBox1.SelectedItem.ToString
sqlCommand.Parameters.Add(New OracleParameter(":mat1", OracleType.VarChar)).Value = strMaterial
Dim dataAdapter As OracleClient.OracleDataAdapter = New OracleClient.OracleDataAdapter(sqlCommand)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Using
End Using
End Function
The ListBox1 is wrong if it is a control. You should use the SelectedItem property as value for the parameter (a bit of error checking is needed though)