从字符串“DGL / C-2905”转换输入'double'无效。 [英] Conversion from string "DGL/C- 2905" to type 'double' is not valid.

查看:128
本文介绍了从字符串“DGL / C-2905”转换输入'double'无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

First Entry is accepted on auto generate ID as DGL/C-2905. But After there is error as "Conversion from string "DGL/C- 2905" to type 'Double' is not valid."

Please advice if i need any correction on my coding.

Data Table column Name ClientID and Datatype - nvarchar(50)





我尝试了什么:





What I have tried:

Private Sub Autogenerated_Id()
        Dim Cmd As New SqlClient.SqlCommand
        Dim Con As New SqlClient.SqlConnection
        Con.ConnectionString = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=dbase;Integrated Security=True;Pooling=False"
        Cmd.Connection = Con
        Con.Open()
        Dim Number As Integer
        Cmd.CommandText = "Select Max(ClientID) from Client"
        If IsDBNull(Cmd.ExecuteScalar) Then
            Number = 2905
            Tbcltid.Text = "DGL/C- " & Number
        Else
            Number = Cmd.ExecuteScalar + 1
            Tbcltid.Text = "DGL/C- " & Number
        End If
        Con.Close()
    End Sub

推荐答案

堆栈跟踪是告诉你错误发生在哪里:

The stack trace is telling you where the error occurs:
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Operators.AddObject(Object Left, Object Right)
at DSTAR_PROJECT.Clients.Autogenerated_Id() in E:\DSTAR-PROJECT\DSTAR-PROJECT\Clients.vb:line 131
at DSTAR_PROJECT.Clients.Cmdcltaddnew_Click(Object sender, EventArgs e) in E:\DSTAR-PROJECT\DSTAR-PROJECT\Clients.vb:line 38



很明显将DGL / C-2905存储到 Tbcltid 系统正在尝试将其转换为<的双倍值code> ClientID ,这显然会失败。问题是,i)为什么你使用texct类型的所有数据库值,和ii)为什么你试图将ID值存储为 double 类型?



鉴于您通过将TextBox内容连接在一起来创建SQL语句,您可能会遇到很多问题。不要这样做,这是非常危险的,让您的系统容易受到SQL注入,以及所有数据的损坏或丢失。



您需要重新编码使用适当的参数变量作为SQL语句的输入,并将源数据正确转换为目标类型。


It is fairly clear that having stored "DGL/C- 2905" into Tbcltid the system is trying to convert it into a double value for ClientID, which will obviously fail. The questions are, i) why are you using texct types for all your database values, and ii) why are you trying to store an ID value as a double type?

And given that you are creating your SQL statement by concatenating TextBox contents together, you are likely to have many problems. Do not do this, it is extremely dangerous and leaves your system vulnerable to SQL injection, and the corruption or loss of all your data.

You need to recode this to use proper parameter variables as input to your SQL statement, and proper conversion of the source data to the destination types.


这篇关于从字符串“DGL / C-2905”转换输入'double'无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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