如何自动生成vb.net中的id列

编程入门 行业动态 更新时间:2024-10-12 03:17:34
本文介绍了如何自动生成vb中的id列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在我的项目数据库中自动生成一个id列,我正在使用以下代码,但是它不起作用. 我在数据库中将"Id"列设置为"Int". 它给了我一个例外,因为从字符串到双精度的转换无效"

I want to auto generate an id column in my project database I am using the following code, but it is not working. I have Set "Id" column as "Int" in my Database. It is giving me an exception as "Conversion From String To Double is Not Valid"

Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click conn.Open() Dim query As String = "insert into Academic(Id,Session)values(@Id,@Session)" If TxtSession.Text = "" Then MsgBox("Please Insert the Session ", MsgBoxStyle.Information, "Grading System") TxtSession.Text = "" txtSessionId.Text = "" TxtSession.Focus() Else Dim query1 As String = "select Id,Session from Academic Where Session='" & TxtSession.Text & "'" cmd1 = New SqlCommand(query1, conn) dr = cmd1.ExecuteReader() If dr.Read = False Then dr.Close() Try Try Dim no As integer Dim k as integer cmd = New SqlCommand("select count(*) from Academic", conn) no = cmd.ExecuteScalar no = CInt(no) + 1 If no > 0 And no <= 9 Then k = ("A000" + no) ElseIf no > 9 And no <= 99 Then k = ("A00" + no) ElseIf no > 99 And no <= 999 Then k = ("A0" + no) Else k = ("A" + no) End If Catch ex As Exception MsgBox(ex.Message) End Try cmd = New SqlCommand(query, conn) dr.Close() cmd.Parameters.AddWithValue("@Id", (k)) cmd.Parameters.AddWithValue("@Session", (TxtSession.Text)) cmd.ExecuteNonQuery() MsgBox("Session Inserted", MsgBoxStyle.Information, "Grading System") ds.Clear() conn.Close() LoadDataInDataGrid() TxtSession.Text = "" TxtSession.Focus() Catch ex As Exception MessageBox.Show(ex.ToString()) Finally If conn.State = ConnectionState.Open Then conn.Close() End If End Try Else MsgBox("Record Exists", MsgBoxStyle.Information, "Grading System") TxtSession.Focus() txtSessionId.Text = "" End If End If conn.Close() End Sub

推荐答案

是因为异常告诉您. 您正在尝试将字符串(k)存储到int字段(Id)中. 将字段更改为字符串或更改代码以生成类型为int的ID. Is it as the exception is telling you. You are trying to store a string (k) into a int field (Id). Either change the field to a string or change the code to generate id of type int.

更多推荐

如何自动生成vb.net中的id列

本文发布于:2023-10-23 14:24:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自动生成   vb   id   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!