Invalidcasrexception是unhandle。从类型'dbnull'到类型'string'的转换无效 [英] Invalidcasrexception was unhandle. Conversion from type 'dbnull' to type 'string' is not valid

查看:90
本文介绍了Invalidcasrexception是unhandle。从类型'dbnull'到类型'string'的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码有什么问题,当我尝试运行时,它表示invalidcastexception:从类型'DBNull'到类型'String'的转换无效。我要做什么



我尝试过:



公开类frm_invoice_a155960



Dim i As String



Private Sub frm_invoice_a155960_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load

refresh_grid()

refresh_grid2()



End Sub



Private Sub refresh_grid()



Dim last_id As String = run_sql_query(SELECT MAX(FLD_ORDER_ID) AS LAST_ID来自TBL_ORDER_LIST_A155960)。行(0).Item(LAST_ID)

Dim last_customer As String = run_sql_query(SELECT MAX(FLD_CUSTOMER_ID)AS LAST_NAME FROM TBL_ORDER_A155960)。行(0) .Item(LAST_NAME)

Dim last_date As String = run_sql_query(SELECT MIN(FLD_ORDER_DATE)AS LAST_DATE FROM TBL_ORDER_A155960)。行(0).Item(LAST_DATE)

lbl_order.Text =& last_id

iname.Text =& last_customer

lbl_date.Text =& last_date



Dim mysql As String =SELECT * FROM TBL_ORDER_LIST_A155960 WHERE FLD_ORDER_ID LIKE%& lbl_order.Text& %

Dim mydatatable As New DataTable

Dim myreader As New OleDb.OleDbDataAdapter(mysql,myconnection)

myreader.Fill( mydatatable)



grd_invoice.DataSource = mydatatable



grd_invoice.Columns(0).HeaderText =ID产品

grd_invoice.Columns(1).HeaderText =ID ORDER

grd_invoice.Columns(2).HeaderText =QUANTITY

grd_invoice.Columns(3).HeaderText =PRICE(RM)

grd_invoice.Columns(4).HeaderText =SUBTOTAL(RM)





结束次级



私人子刷新_grid2()



Dim total As String = 0

For I Integer = 0 to grd_invoice.RowCount - 1

total + = grd_invoice.Rows(I).Cells(4) .Value



下一页



lbl_total.Text =RM&总计



结束子

结束班

What is wrong with the coding, when i try to run, it say invalidcastexception : Conversion from type 'DBNull' to type 'String' is not valid. what i have to do

What I have tried:

Public Class frm_invoice_a155960

Dim i As String

Private Sub frm_invoice_a155960_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
refresh_grid()
refresh_grid2()

End Sub

Private Sub refresh_grid()

Dim last_id As String = run_sql_query(" SELECT MAX(FLD_ORDER_ID) AS LAST_ID FROM TBL_ORDER_LIST_A155960").Rows(0).Item("LAST_ID")
Dim last_customer As String = run_sql_query(" SELECT MAX(FLD_CUSTOMER_ID) AS LAST_NAME FROM TBL_ORDER_A155960").Rows(0).Item("LAST_NAME")
Dim last_date As String = run_sql_query(" SELECT MIN(FLD_ORDER_DATE) AS LAST_DATE FROM TBL_ORDER_A155960").Rows(0).Item("LAST_DATE")
lbl_order.Text = "" & last_id
iname.Text = "" & last_customer
lbl_date.Text = "" & last_date

Dim mysql As String = " SELECT * FROM TBL_ORDER_LIST_A155960 WHERE FLD_ORDER_ID LIKE ""%" & lbl_order.Text & "%"""
Dim mydatatable As New DataTable
Dim myreader As New OleDb.OleDbDataAdapter(mysql, myconnection)
myreader.Fill(mydatatable)

grd_invoice.DataSource = mydatatable

grd_invoice.Columns(0).HeaderText = "ID PRODUCT"
grd_invoice.Columns(1).HeaderText = "ID ORDER"
grd_invoice.Columns(2).HeaderText = "QUANTITY"
grd_invoice.Columns(3).HeaderText = "PRICE (RM)"
grd_invoice.Columns(4).HeaderText = "SUBTOTAL (RM)"


End Sub

Private Sub refresh_grid2()

Dim total As String = 0
For I As Integer = 0 To grd_invoice.RowCount - 1
total += grd_invoice.Rows(I).Cells(4).Value

Next

lbl_total.Text = "RM " & total

End Sub
End Class

推荐答案

首先,不要不这样做。

永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



其次,要解决您的问题,请修复您的数据库。表中的一个或多个列至少有一个为null的条目,并且正在返回该条目。这从SQL返回DBNull.Value并且不能更改为字符串。最好的解决方法是找出null的位置 - 它可以在几个地方中的任何一个 - 使用调试器并找出数据库包含NULL值并修复它们的原因。
我们不能为你做到这一点:我们没有你的代码,如果我们有它,我们不知道如何使用它,我们没有你的数据。所以试试吧 - 看看你能找到多少信息!
First off, don't do it like that.
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use parametrized queries instead.

Secondly, to get rid of your problem, fix your database. One or more columns in your table has at least one entry which is null, and that is being returned. This arrives back from SQL as DBNull.Value and that can't be changed into a string. The best fix is to find out where the null is - and it could be in any of several places - using the debugger and find out why your DB contains values that are NULL, and fixing them.
We can't do that for you: we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!


这篇关于Invalidcasrexception是unhandle。从类型'dbnull'到类型'string'的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆