HOME > .NET Framework > Forum > โค้ด insert ข้อมูล ช่วยตรวจสอบให้ด้วยนะครับ ขึ้น errer ว่า ExecuteNonQuery: CommandText property has not been initialized
โค้ด insert ข้อมูล ช่วยตรวจสอบให้ด้วยนะครับ ขึ้น errer ว่า ExecuteNonQuery: CommandText property has not been initialized
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class Form5
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If MessageBox.Show("Exit Form Register?", "", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Me.Close()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim CommandText As String = ""
If Me.txtName.Text = "" Then
MessageBox.Show("Please input (Name)")
Me.txtName.Focus()
Exit Sub
End If
If Me.txtPass.Text = "" Then
MessageBox.Show("Please input (Pass)")
Me.txtPass.Focus()
Exit Sub
End If
If Me.rdoOperater.Checked = False And Me.rdoTechnician.Checked = False Then
MessageBox.Show("Please input (Position)")
Exit Sub
End If
Dim myConnection As SqlConnection
myConnection = New SqlConnection("Server=saengtawan-pc\sql2008;UID=sa;PASSWORD=300881;Database=mydatabase;Max Pool Size=400;Connect Timeout=600;")
myConnection.Open()
Dim myCommand As SqlCommand = myConnection.CreateCommand()
CommandText = "INSERT INTO [mytable] ([Username], [Password], [Position])"
CommandText &= " VALUES ('" & Me.txtName.Text & "',"
CommandText &= "'" & Me.txtPass.Text & "',"
If Me.rdoOperater.Checked = True Then
CommandText &= "'Operator'"
ElseIf Me.rdoTechnician.Checked = True Then
CommandText &= "'Technician'"
End If
CommandText &= "')'"
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Save Successfully.")
Me.Hide()
Dim f As New Form3
f.Show()
End Sub
End Class