请帮我解决这个异常错误 [英] Please help me to solve this Exception error
问题描述
波纹管代码可以正常工作但插入数据后仍然显示此错误:将数据类型varchar转换为数字时出错。
请帮忙。
Imports System.Data.SqlClient
Public 类 Form1
Dim connection 作为 SqlConnection
Dim ds As DataSet
Dim dt As DataTable
Dim da As SqlDataAdapter
Dim sql As 字符串
Dim cmb As SqlCommand
Dim sBuilder As SqlComman dBuilder
私有 Sub Form1_Load( ByVal 发件人作为系统。对象, ByVal e As System.EventArgs)句柄 MyBase .Load
DataGridView1.Columns( 5 )。DefaultCellStyle.Format =(< span class =code-string> n2)
DataGridView1.Columns( 6 )。DefaultCellStyle.Format =( n2)
DataGridView1.Columns ( 0 )。DefaultCellStyle.Format = 日期。现在
结束跨度> Sub
私有 Sub Button1_Click( ByVal sender As System。对象, ByVal e 作为 System.EventArgs)句柄 Button1.Click
尝试
Dim connetionString 作为 字符串
Dim connection 作为 SqlConnection
Dim adapter 正如 新 SqlDataAdapter
Dim sql As String
Dim Ref_No,Account_Code,Account_Name,Details,Debit,Credit As String
Dim 值作为 字符串 = 2000-02-02
Dim Date_ < span class =code-keyword> As DateTime = 日期 .Parse(value)
For i As 整数 = 0 我 .DataGridView1.Rows.Count
Date_ = Me .DataGridView1.Rows(i).Cells( 0 )。值
Ref_No = Me .DataGridView1.Rows(i).Cells( 1 )。值
Account_Code = Me .DataGridView1.Rows(i).Cells( 2 )。值
Account_Name = < span class =code-keyword> Me .DataGridView1.Rows(i).Cells( 3 )。值
Details = Me .DataGridView1.Rows(i).Cells( 4 )。值
Debit = Me .DataGridView1.Rows(i).Cells( 5 )。值
Credit = Me .DataGridView1.Rows(i).Cells( 6 )。值
connetionStri ng = 数据源=。\sqlexpress;初始目录= financialaccounting; Integrated Security = True
connection = 新 SqlConnection(connetionString)
sql = 插入General_Ledger(Date_,Ref_No,Account_Code,Account_Name,Details,Debit,Credit)值('&日期_& ','& Ref_No& ','& Account_Code& ','& Account_Name& ','&细节& ','&借记卡 ','&信用与优惠 ')
connection.Open()
adapter.InsertCommand = 新 SqlCommand(sql,connection)
adapter.InsertCommand.ExecuteNonQuery()
下一步
MsgBox( < span class =code-string>插入行!!)
Catch oex 作为异常
MsgBox(oex.Message)
结束 尝试
结束 Sub
< span class =code-keyword>私有 Sub Button2_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs)< span class =code-keyword>句柄 Button2.Click
Dim connectionString 正如 字符串 = 数据源= 。\sqlexpress; Initial Catalog = financialaccounting; Integrated Security = True
Dim sql As 字符串 = SELECT Date_,Ref_No, Account_Code,Account_Name,Details,Debit,来自General_Ledger的信用额
Dim connection As 新 SqlConnection(connectionString)
connection.Open()
cmb = 新 SqlCommand(sql,connection)
da = 新 SqlDataAdapter(cmb)
sBuilder = 新 SqlCommandBuilder(da)
ds = 新 DataSet()
da.Fill (ds, General_Ledger)
dt = ds.Tables( General_Ledger)
connection.Close()
DataGridView1.DataSource = ds.Tables( General_Ledger)
DataGridView1.Columns( 5 ).DefaultCellStyle.Format =( n2 )
DataGridView1.Columns( 6 )。DefaultCellStyle.Format =( n2)
DataGridView1.Columns( 0 )。DefaultCellStyle.Format = 日期。现在
结束 Sub
希望您的借方和贷方都是号码。所以你可以这样写。
' 借记卡part
Debit = Me .DataGridView1.Rows(i).Cells( 5 )。值
' 这里是修改版本
Debit = ctype ( Me .DataGridView1.Rows(i).Cells( 5 )。值, double )
信用部分
Credit = 我。 DataGridView1.Rows(i).Cells( 6 )。值
' 以下是修改信用证版本
Credit = ctype ( Me .DataGr idView1.Rows(i).Cells( 6 )。值, double )
我假设借记和贷记为双重类型数据,但如果你使用long只需在代码中使用long而不是doulbe。
< blockquote>因为,我不知道您传递给Sql Server的列数据类型。在这种情况下,您显然在 varchar
列中有一些无法转换为数字类型的内容。此错误消息清楚地解释了这一点。
解决方案并非如此艰难。您需要检查以下内容:
解决方案:1。查看数据。
2.在varchar列中找到非数字值。
3.更改/删除这些值。
您可以在Google中搜索错误消息并修复它。
The bellow code work all right but after inserting the data it keeps showing this error: Error converting data type varchar to numeric.
Please help.
Imports System.Data.SqlClient
Public Class Form1
Dim connection As SqlConnection
Dim ds As DataSet
Dim dt As DataTable
Dim da As SqlDataAdapter
Dim sql As String
Dim cmb As SqlCommand
Dim sBuilder As SqlCommandBuilder
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns(5).DefaultCellStyle.Format = ("n2")
DataGridView1.Columns(6).DefaultCellStyle.Format = ("n2")
DataGridView1.Columns(0).DefaultCellStyle.Format = Date.Now
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim connetionString As String
Dim connection As SqlConnection
Dim adapter As New SqlDataAdapter
Dim sql As String
Dim Ref_No, Account_Code, Account_Name, Details, Debit, Credit As String
Dim value As String = "2000-02-02"
Dim Date_ As DateTime = Date.Parse(value)
For i As Integer = 0 To Me.DataGridView1.Rows.Count
Date_ = Me.DataGridView1.Rows(i).Cells(0).Value
Ref_No = Me.DataGridView1.Rows(i).Cells(1).Value
Account_Code = Me.DataGridView1.Rows(i).Cells(2).Value
Account_Name = Me.DataGridView1.Rows(i).Cells(3).Value
Details = Me.DataGridView1.Rows(i).Cells(4).Value
Debit = Me.DataGridView1.Rows(i).Cells(5).Value
Credit = Me.DataGridView1.Rows(i).Cells(6).Value
connetionString = "Data Source=.\sqlexpress;Initial Catalog=financialaccounting;Integrated Security=True"
connection = New SqlConnection(connetionString)
sql = "insert into General_Ledger (Date_,Ref_No,Account_Code,Account_Name,Details,Debit,Credit) values('" & Date_ & "','" & Ref_No & "','" & Account_Code & "','" & Account_Name & "','" & Details & "','" & Debit & "','" & Credit & "')"
connection.Open()
adapter.InsertCommand = New SqlCommand(sql, connection)
adapter.InsertCommand.ExecuteNonQuery()
Next
MsgBox("Row inserted !! ")
Catch oex As Exception
MsgBox(oex.Message)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim connectionString As String = "Data Source=.\sqlexpress;Initial Catalog=financialaccounting;Integrated Security=True"
Dim sql As String = "SELECT Date_,Ref_No,Account_Code,Account_Name,Details,Debit,Credit from General_Ledger"
Dim connection As New SqlConnection(connectionString)
connection.Open()
cmb = New SqlCommand(sql, connection)
da = New SqlDataAdapter(cmb)
sBuilder = New SqlCommandBuilder(da)
ds = New DataSet()
da.Fill(ds, "General_Ledger")
dt = ds.Tables("General_Ledger")
connection.Close()
DataGridView1.DataSource = ds.Tables("General_Ledger")
DataGridView1.Columns(5).DefaultCellStyle.Format = ("n2")
DataGridView1.Columns(6).DefaultCellStyle.Format = ("n2")
DataGridView1.Columns(0).DefaultCellStyle.Format = Date.Now
End Sub
hope your debit and credit both are number. So you can write it like this.
' Debit part Debit = Me.DataGridView1.Rows(i).Cells(5).Value ' here is the modify version Debit = ctype(Me.DataGridView1.Rows(i).Cells(5).Value,double)
'Credit part Credit = Me.DataGridView1.Rows(i).Cells(6).Value ' Here is the modify credit version Credit = ctype(Me.DataGridView1.Rows(i).Cells(6).Value,double)
I assume debit and credit as double type data but if you use long just use long instead of doulbe in the code.
Since, I don't know the column data types, which you are passing to Sql Server. Here in this case, you obviously have something invarchar
column that can't be converted to numeric type. This error message explains it clearly.
The solution is not so tough. You need to check for the following:
Solution:1. Look at the data.
2. Find the non-numeric values in the varchar column.
3. Change/Remove those values.
You could have searched your error message in Google and fixed it.
--Amit
这篇关于请帮我解决这个异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!