vb.net getting value from a new name sql database - sql

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

Related

Checking access database vb.net

I am trying to compare the values in my dictionary which contains values/quantities to a standard database containing similar values/quantities. I want to check if the value matches the database, then check if the quantity is less than the database.
I keep getting an error when I run the code on the "Using myreader" line, with the following: System.Data.OleDb.OleDbException: 'IErrorInfo.GetDescription failed with E_FAIL(0x80004005).' This is my first time using OleDBDatareader so perhaps I am doing something incorrectly. Is this not allowed, I assumed comparing a list a values in a dictionary should be possible.
Dim myconnection As OleDbConnection
Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\UsersTables.accdb; Persist Security Info=False"
myconnection = New OleDbConnection(constring)
myconnection.Open()
Dim keepSell As String
For Each KeyPair In colSums
Dim sqlQry As String
sqlQry = "SELECT Part,Quantity FROM PartsList WHERE Item = '" & keyPair.Key & "'AND Size= '" & keyPair.Value & "'"
Dim cmd As New OleDbCommand(sqlQry, myconnection)
Using myreader As OleDbDataReader = cmd.ExecuteReader()
If myreader.Read() Then
keepSell = "Keep"
Else
keepSell= "Sell"
End If
'myreader.Close()
End Using
DataGridView1.Rows.Add(KeyPair.Key, KeyPair.Value, keepSell)
Next

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.

multiple parameter selection in crystal report in .net not working

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;

Insert new records into a table with Visual Basic using OLEDBConnection

I'm using Visual Basic 2010 Express and Access 2003.
I'm trying to make sql querys to the mdb file. I'm using OLEDBConnection. The Select query works fine, but I can't insert rows in the table. Here is the code.
Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0;data source=" & System.IO.Directory.GetCurrentDirectory() & "\tpv.mdb;"
Dim con As New OleDb.OleDbConnection(connStr)
con.Open()
Dim query As String = "select * from Productos"
Dim cmd As New OleDb.OleDbCommand(query, con)
Dim reader As OleDb.OleDbDataReader
reader = cmd.ExecuteReader
While reader.Read()
MsgBox(reader.GetValue(0) & ", " & reader.GetValue(1) & " , " & reader.GetValue(2))
End While
reader.Close()
query = "insert into Productos (NombreProducto,PrecioCoste) VALUES ('cana',4)"
Dim cmd2 As New OleDb.OleDbCommand(query, con)
cmd.ExecuteNonQuery()
con.Close()
Why the INSERT query does not work?
Ok, I found my stupid problem.
Althoug I have declared 2 OleDbCommands, I referenced the first one in both cases

help date format in vb.net

Dim Con As OleDb.OleDbConnection
Dim Sql As String = Nothing
Dim Reader As OleDb.OleDbDataReader
Dim ComboRow As Integer = -1
Dim Columns As Integer = 0
Dim Category As String = Nothing
Dim oDatumMin As Date
Dim column As String
column = Replace(TxtDateMax.Text, "'", "''")
'oDatumMin = Convert.ToDateTime(TxtDateMin.Text)
oDatumMin = DtpMin.Value
Dim oPath As String
oPath = Application.StartupPath
' Select records.
Con = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & oPath & "\trb.accdb;")
Dim cmd As New OleDb.OleDbCommand
'Dim data_reader As OleDbDataReader = cmd.ExecuteReader()
Sql = ("SELECT * FROM " & cmbvalue & " WHERE Datum>='" & oDatumMin & "'")
cmd = New OleDb.OleDbCommand(Sql, Con)
Con.Open()
Reader = cmd.ExecuteReader()
Do While Reader.Read()
Dim new_item As New ListViewItem(Reader.Item("Datum").ToString)
new_item.SubItems.Add(Reader.Item("Steleks i krpe za cišcenje-toal papir-ubrusi-domestos").ToString)
new_item.SubItems.Add(Reader.Item("TEKUCINA ZA CIŠCENJE PLOCICA").ToString)
new_item.SubItems.Add(Reader.Item("KESE ZA SMECE").ToString)
new_item.SubItems.Add(Reader.Item("OSTALO-džoger-spužva za laminat").ToString)
new_item.SubItems.Add(Reader.Item("PAPIR").ToString)
LvIzvjestaj.Items.Add(new_item)
Loop
' Close the connection.strong text
Con.Close()``
when i select table,(cmbvalue) from combobox
and when i select date from datetime picker (dtp) or in last case from texbox converted to date and time sql looks like this "SELECT * FROM Uprava WHERE Datum>='2.6.2010 10:28:14'"
and all query looks ok but am geting
Data type mismatch in criteria expression. error for date (oDatumMin) when excute
column in access is also set to date
i have no idea what else to try
I suspect this is a localisation issue. Try changing the date format to one which should be universally recognised:
SELECT * FROM Uprava WHERE Datum >='2010/06/02 10:28:14'
[What is your server locale/code page?]
Dim sql As String = "SELECT * FROM MyTable WHERE Datum >= ?"
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql)
cmd.Parameters.Add(New OleDb.OleDbParameter("#Datum", OleDb.OleDbType.Date))
cmd.Parameters("Datum").Value = oDatumMin
http://msdn.microsoft.com/de-de/library/bbw6zyha.aspx