附加信息:从“Int32"到“DateTime"的无效转换 [英] Additional information: Invalid cast from 'Int32' to 'DateTime'
本文介绍了附加信息:从“Int32"到“DateTime"的无效转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了另一个代码.
这是我使用的所有代码...
this is all the codes that I'm using...
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable
Public Class Form1
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim addstring As String
Dim cnn As OleDbConnection = New OleDbConnection
Dim ds As DataSet = New DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection = ds.Tables
Dim cmd As New OleDb.OleDbCommand
Dim dr As System.Data.OleDb.OleDbDataReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
t_date.Text = Today
provider = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
dataFile = "C:\Users\hp-2\Documents\Visual Studio 2012\Projects\Delta\Delta.mdb"
connString = provider & dataFile
cnn.ConnectionString = connString
da = New OleDbDataAdapter("Select Customer_Name, Job, Amount from [Transaction]
where Trans_date = Date()", cnn)
da.Fill(ds, "Transaction")
Dim view1 As New DataView(tables(0))
Dim source1 As New BindingSource()
source1.DataSource = view1
showdata.DataSource = view1
showdata.Refresh()
cnn.Close()
End Sub
Private Sub btmclose_Click(sender As Object, e As EventArgs) Handles btmclose.Click
Me.Close()
End Sub
Private Sub C_job_SelectedIndexChanged(sender As Object, e As EventArgs) Handles C_job.SelectedIndexChanged
Dim selected As String = C_job.SelectedItem.ToString()
If selected = "Internet" Then
t_amount.Text = "20"
php.Visible = True
ElseIf selected = "Games" Then
t_amount.Text = "10"
php.Visible = True
ElseIf selected = "Print (short)" Then
t_amount.Text = "1"
php.Visible = True
ElseIf selected = "Print (long)" Then
t_amount.Text = "2"
php.Visible = True
ElseIf t_amount.Text = "" Then
php.Visible = False
End If
End Sub
Private Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
dataFile = "C:\Users\hp-2\Documents\Visual Studio 2012\Projects\Delta\Delta.mdb"
connString = provider & dataFile
cnn.ConnectionString = connString
cnn.Open()
cmd.Connection = cnn
addstring = "insert into Transaction (Customer_Name, Job, Trans_date, Amount ) " & _
" values (@cname, @job, @tdate, @amount)"
Using conn As New OleDbConnection(connString)
Using comm As New OleDbCommand()
With comm
.Connection = conn
.CommandType = CommandType.Text
.CommandText = addstring
.Parameters.AddWithValue("@cname", C_name.Text)
.Parameters.AddWithValue("@job", C_job.Text)
.Parameters.AddWithValue("@date", Convert.ToDateTime(t_date.Text.Length))
.Parameters.AddWithValue("@amount", Convert.ToInt64(t_amount.Text))
End With
Try
conn.Open()
comm.ExecuteNonQuery()
Catch ex As Exception
End Try
End Using
End Using
cmd.ExecuteNonQuery()
cnn.Close()
End Sub
End Class
错误:
附加信息:从Int32"到DateTime"的无效转换.
我的表名是 Transaction (Transaction_Id, Customer_name, Job, t_date, amount)--> ms-access 中的 Transaction_Id 是 AutoIncrement ..
my table name is Transaction (Transaction_Id, Customer_name, Job, t_date, amount) --> the Transaction_Id is AutoIncrement in the ms-access..
所以我项目中的文本框是:
- 客户姓名,
- 金额,
- t_date
和组合框是:
- 工作
推荐答案
您的问题似乎在这一行:
Your issue seems to be on this line:
Convert.ToDateTime(t_date.Text.Length)
您试图将文本字段(Int32
)的长度转换为 DateTime
,但这是行不通的.
You trying to converting the length of a text field (an Int32
) to a DateTime
, which isn't going to work.
您可能只想做:
Convert.ToDateTime(t_date.Text)
参考资料示例:MSDN Convert.ToDateTime
这篇关于附加信息:从“Int32"到“DateTime"的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文