I have 4 Datetimepicker. the 2 datetimepicker is on dateformat(datefrom,dateto) and the other 2 datetimepicker is on Timeformat(timefrom,timeto).
I want to search a table that display only in datagridview that will display only start on Date from and time from to date to and time to... But my code doenst work.
here's my code:
Dim fromto As String = String.Empty
fromto &= "Select * from setplan where endplan >='" & dtimefrom.Text & "'and endplan >='" & dtimeto.Text & "' and [Dateinput] >='" & ddatefrom.Text & "' and [dateinput] <= '" & ddateto.Text & "'"
Dim connection As String = ("provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Documents\planning.accdb;Persist Security Info=False;")
Using conn As New OleDb.OleDbConnection(connection)
Using cmd As New OleDb.OleDbCommand(fromto)
With cmd
.Connection = conn
.CommandType = CommandType.Text
.CommandText = fromto
End With
Try
conn.Open()
cmd.ExecuteNonQuery()
Dim da As New OleDb.OleDbDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
If ds.Tables.Count > 0 Then
DataGridView1.DataSource = ds.Tables(0)
End If
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using
End Using
End Sub
You need formatted string expressions for your date/time values:
fromto &= "Select * from setplan where endplan >= #" & ddateto.Value.ToString("yyyy'/'MM'/'dd") & " " & dtimeto.Value.ToString("HH':'mm") & "# and [dateinput] >= #" & ddatefrom.Value.ToString("yyyy'/'MM'/'dd") & " " & dtimefrom.Value.ToString("HH':'mm") & "#"
i am learning asp.net using vb,i am working on college assignment
i have to read data from msaccess and show record in label
basically i am doing 'search' operation
here is the code
Public Class WebForm2
Inherits System.Web.UI.Page
Dim connection As New OleDb.OleDbConnection
Dim sname, lname, city, subj, gender, hobbies, religion As String
Dim age, phone As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
connection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\himzz\Documents\Database1.accdb;")
connection.Open()
If Not IsPostBack Then
End If
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
sname = TextBox1.Text
lname = TextBox2.Text
age = TextBox3.Text
city = TextBox4.Text
phone = TextBox5.Text
subj = DropDownList1.SelectedValue
If RadioButton1.Checked Then
gender = "male"
ElseIf RadioButton2.Checked Then
gender = "female"
Else
gender = "other"
End If
'If CheckBox1.Checked And CheckBox2.Checked And CheckBox3.Checked Then
' hobbies = "cricket swimming tennis "
'End If
If CheckBox1.Checked Then
hobbies += "cricket "
End If
If CheckBox2.Checked Then
hobbies += "swimming "
End If
If CheckBox3.Checked Then
hobbies += " tennis "
End If
religion = DropDownList2.SelectedValue
Try
Dim q As String = "insert into stud (sname,lname,age,city,phone,subj,gender,hobbies,religion) values('" & sname & "','" & lname & "','" & age & "','" & city & "','" & phone & "','" & subj & "','" & gender & "','" & hobbies & "','" & religion & "' )"
Dim cmd As New OleDb.OleDbCommand(q, connection)
cmd.ExecuteNonQuery()
MsgBox("submitted")
Catch ex As Exception
MsgBox("error")
End Try
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim q As String = "select lname from stud where sname='" & sname & "' "
Dim cmd As New OleDb.OleDbCommand(q, connection)
Dim reader As OleDb.OleDbDataReader
reader = cmd.ExecuteReader()
While reader.Read()
(tried this but not worked) ' Label12.Text = " " & reader("sname")
(tried this also but not worked)' Label12.Text =reader.Item("sname").ToString()
(not working) Label12.Text = reader("sname").ToString
'Label13.Text = "age " & reader("age")
'Label14.Text = "city " & reader("city")
'Label15.Text = "phone no " & reader("phone")
'Label16.Text = "subject " & reader("subj")
'Label17.Text = "Gedner " & reader("gender")
'Label18.Text = "Hobbies " & reader("hobbies")
'Label19.Text = "Religion " & reader("religion")
End While
reader.Close()
End Sub
End Class
Imports System.Data
Imports System.Data.OleDb
Public Class form7
Dim inc As Integer
Dim con As New OleDb.OleDbConnection
Dim cmd As OleDbCommand
Dim dbprovider As String
Dim dbsource As String
Dim str As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim TotalRows As Integer
Dim ID_Number As Integer
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Bank\db1.accdb ")
con.Open()
Dim cmd As New OleDbCommand("SELECT COUNT(*) FROM accountsTable", con)
Dim totalRows As Long = CInt(cmd.ExecuteScalar())
cmd.ExecuteNonQuery()
TextBox1.Text = totalRows + 1000
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Form5.Show()
dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0; dbsource=Data Source=D:\Bank\db1.accdb"
MsgBox("Database is now open")
str = "Insert into accountsTable (Account_Number, First_Name, Midle_Name, Last_Name, Date_of_birth, Occupation, Age, Marital_Status, Address, City, Country, State, Account_Type, Pin_Code, Mobile_Number, Email_ID, Opening_Balance) values ('" &
TextBox1.Text & "','" &
TextBox2.Text & "','" &
TextBox3.Text & "','" &
TextBox4.Text & "','" &
DateTimePicker1.Value & "','" &
TextBox6.Text & "','" &
TextBox7.Text & "','" &
TextBox8.Text & "','" &
TextBox9.Text & "','" &
ComboBox1.Text & "','" &
ComboBox2.Text & "','" &
ComboBox3.Text & "','" &
ComboBox4.Text & "','" &
TextBox10.Text & "','" &
TextBox11.Text & "','" &
TextBox12.Text & "','" &
TextBox13.Text & "','" & "')"
cmd = New OleDb.OleDbCommand(str, con)
cmd.ExecuteNonQuery()
MsgBox("Record is inserted")
con.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
DateTimePicker1.Value = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
ComboBox1.Text = ""
ComboBox2.Text = ""
ComboBox3.Text = ""
ComboBox4.Text = ""
TextBox10.Text = ""
TextBox11.Text = ""
TextBox12.Text = ""
TextBox13.Text = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
Form3.Show()
End Sub
End Class
The error happens here :
cmd = New OleDb.OleDbCommand(str, con)
cmd.ExecuteNonQuery() ' <-- error here
MsgBox("Record is inserted")
con.Close()
You have 17 columns in your columns list, but you are adding an extra (18th) zero-length string value onto the end of your VALUES list:
TextBox13.Text & "','" & "')"
^^^^^^^^
You really should be using a parameterized query, something more like this:
str =
"Insert into accountsTable (Account_Number, First_Name, Midle_Name, Last_Name, Date_of_birth, Occupation, Age, Marital_Status, Address, City, Country, State, Account_Type, Pin_Code, Mobile_Number, Email_ID, Opening_Balance) " &
"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
cmd = New OleDb.OleDbCommand(str, con)
cmd.Parameters.AddWithValue("?", TextBox1.Text)
cmd.Parameters.AddWithValue("?", TextBox2.Text)
cmd.Parameters.AddWithValue("?", TextBox3.Text)
cmd.Parameters.AddWithValue("?", TextBox4.Text)
cmd.Parameters.AddWithValue("?", DateTimePicker1.Value)
cmd.Parameters.AddWithValue("?", TextBox6.Text)
cmd.Parameters.AddWithValue("?", TextBox7.Text)
cmd.Parameters.AddWithValue("?", TextBox8.Text)
cmd.Parameters.AddWithValue("?", TextBox9.Text)
cmd.Parameters.AddWithValue("?", ComboBox1.Text)
cmd.Parameters.AddWithValue("?", ComboBox2.Text)
cmd.Parameters.AddWithValue("?", ComboBox3.Text)
cmd.Parameters.AddWithValue("?", ComboBox4.Text)
cmd.Parameters.AddWithValue("?", TextBox10.Text)
cmd.Parameters.AddWithValue("?", TextBox11.Text)
cmd.Parameters.AddWithValue("?", TextBox12.Text)
cmd.Parameters.AddWithValue("?", TextBox13.Text)
cmd.ExecuteNonQuery()
Note that if some of those TextBox controls may be empty then you'll have to check the length of their .Text properties and possibly insert a null value instead. For example, for "Opening_Balance" you may need to do something like
If TextBox13.Text.Length = 0 Then
cmd.Parameters.AddWithValue("?", DBNull.Value)
Else
cmd.Parameters.AddWithValue("?", Convert.ToDecimal(TextBox13.Text))
End If
In my project i display an 3 droplists (Daydrplist, MonthDropList, YearDropList). When the user selects the values and clicks the button the values should be inserted to the access database in the BirthDate column of the Teacher table.
How can I do that?
Protected Sub NextBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles NextBtn.Click
con = New OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\user\Desktop\web programming\\IULWebInformationSystem1\IULWebInformationSystem1\App_Data\IUlDB.accdb;Persist Security Info=True"
con.Open()
sqlquery = "insert into Teacher values ('" & FirstNameTxtBox.Text & "','" & MiddelNameTxtBox.Text & "','" & LastNameTxtBox.Text & "','" & POBTxtBox.Text & "','" & NationalityDropList.SelectedValue & "','" & Gendrplist.SelectedValue & "')"
cmd = New OleDbCommand(sqlquery, con)
cmd.ExecuteNonQuery()
con.Close()
FirstNameTxtBox.Text = ""
MiddelNameTxtBox.Text = ""
LastNameTxtBox.Text = ""
POBTxtBox.Text = ""
NationalityDropList.SelectedValue = ""
Gendrplist.SelectedValue = ""
lblmsg2.Text = "Personal Information was Added Successfully"
lblmsg2.Visible = True
Response.Redirect("PersonalAddress.aspx")
End Sub
I have this code and its coming up with an INSERT INTO statement error...
Its probably something but I have been at it for a while... please help.
'Add items to db'
Function recordOrder()
objDT = Session("Cart")
Dim intCounter As Integer
For intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath2 & ";"
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
myCommand2.CommandText = "INSERT INTO order(order_date, coupon_id, customer_id, quantity) values('" & System.DateTime.Now & "','" & Int32.Parse(objDR("ID")) & "','" & Int32.Parse(custID) & "','" & Int32.Parse(objDR("quantity")) &"')"
myCommand2.Connection = con2
con2.Open()
myCommand2.ExecuteReader()
con2.Close()
test.Text += "Order ID: " & objDR("ID") & "Order Date: " & System.DateTime.Now & ", Cust ID: " & custID & ", Quantity: " & objDR("quantity") &" "
Next
End Function
I think you are getting an error by not enclosing the Date inside Pound signs. You have to do this in Jet (Access) when using variables not parameters.
VALUES('#" & DateTime.Now.Date & "#',...
I also took the liberty of refactoring this code for you since you are creating a new connection for each record which is bad news. Use a Try Catch Finally block and move all that stuff outside the For Loop (please see below)
Function recordOrder()
objDT = Session("Cart")
Dim intCounter As Integer
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" <-- etc
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
myCommand2.Connection = con2
con2.Open()
Try
For intCounter = 0 To obDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
myCommand2.CommandText = "INSERT INTO order(order_date,coupon_id,customer_id,quantity)" _
& "VALUES ('#" & System.DateTime.Now.Date & "#','" & Int32.Parse(objDR("ID")) & "','" & Int32.Parse(custID) _
& "','" & Int32.Parse(objDR("quantity")) & "')"
myCommand2.ExecuteReader()
Next
Catch ex As Exception
'handle errors here
Finally
If con2.State = ConnectionState.Open Then
con2.Close()
End If
End Try
End Function
Remember to mark as answered if this helps.
I've sorted it out by removing the single quotes. Thanks everybody to contributed to this.