This is VB.net executes some SQL code:
Dim dc As New SqlCommand
Dim Strsql As String = ""
dc.Connection = cnn_myself
Strsql = "SET IDENTITY_INSERT gprs_route.dbo.landmark ON;
Insert into gprs_route.dbo.landmark ( Landmark_ID , Landmark_Name , Area_ID , Class_ID , Lon , Lan ) values (4189,'test1121',1,0,121432512,24967338);
SET IDENTITY_INSERT gprs_route.dbo.landmark OFF;"
dc.CommandText = Strsql
dc.ExecuteNonQuery()
it will crash at dc.ExecuteNonQuery() and the application will popup the error message dialog show below:
System.InvalidCastException:
從字串 "資料表 'gprs_route.dbo.landmark' 不具" 至型別 'Boolean' 的轉換是無效的。
(Converting from string table 'gprs_route.dbo.landmark' to type Boolean is useless)
System.FormatException: 輸入字串格式不正確(the input string format is not correct)。
於 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
於 Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
--- 內部例外狀況堆疊追蹤的結尾(Inner exception stacktrace end) ---
於 Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
於 ITMIS.System_Setup.Btn_Yes_Click(Object sender, EventArgs e)
於 System.Windows.Forms.Control.OnClick(EventArgs e)
於 Infragistics.Win.UltraControlBase.OnClick(EventArgs e)
於 Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)
於 Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)
於 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
於 System.Windows.Forms.Control.WndProc(Message& m)
於 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
於 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
於 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
This is the table schema:
CREATE TABLE [dbo].[landmark]
(
[Landmark_ID] int NOT NULL IDENTITY(3064,1) ,
[Landmark_Name] nvarchar(150) NULL ,
[Area_ID] int NULL ,
[Class_ID] int NULL ,
[Lon] int NULL ,
[Lan] int NULL
)
This is full function code below
Public Function Landmark_Insert(ByVal Data() As String) As String
Dim r As SqlDataReader
Dim c As New SqlCommand("Select max(Landmark_ID) as Landmark_ID from gprs_route.dbo.landmark", cnn_myself)
r = c.ExecuteReader
r.Read()
Data(0) = (Convert.ToInt32(r.Item("Landmark_ID").ToString) + 1).ToString()
Dim dc As New SqlCommand
Dim Strsql As String = ""
Select Case Data.Length
Case 4
Strsql += "SET IDENTITY_INSERT gprs_route.dbo.landmark ON;"
Strsql += "Insert into gprs_route.dbo.landmark ( "
Strsql += "Landmark_ID , "
Strsql += "Landmark_Name , "
Strsql += "Area_ID , "
Strsql += "Class_ID , "
Strsql += "Lon , "
Strsql += "Lan ) "
Strsql += "values (" & Data(0) & ",'" & Data(1) & "'," & Data(2) & "," & Data(3) & "," & 121432512 & "," & 24967338 & ");"
Strsql += "SET IDENTITY_INSERT gprs_route.dbo.landmark OFF;"
MessageBox.Show(Strsql)
dc.Connection = cnn_myself
dc.CommandText = Strsql
Case 6
'it won't call this....
End Select
Try
MessageBox.Show("1")
dc.ExecuteNonQuery() ' crash and show Exception error dialog
MessageBox.Show("2")
Catch ex As Exception
Return ex.Message
End Try
End Function
How to fix the crash problem?
dc.CommandText = Strsqlcome after the line whereStrsqlis assignedSET IDENTITY_INSERT...? - PhylypSqlClientnamespace in that stack trace, I suspect you've misidentified the source of the error. As such, any speculation about the SQL code shown is pointless. - Damien_The_UnbelieverBtn_Yes_Clickseen in the stack trace andLandmark_Insertthat you've provided above? - Phylyp