Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim Conn As SqlConnection
Dim Cmd As SqlCommand
Dim Cmd1 As SqlCommand
Dim dtReader As SqlDataReader
Dim sql As String
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If txtaddress.Text <> "" Then
Try
Dim sqlx As String = "select count(cat_id) as count_ from tb_category where cat_name = '" & Me.txtaddress.Text & "'"
Cmd1 = New SqlCommand(sqlx, Conn)
dtReader = Cmd1.ExecuteReader()
dtReader.Read()
Dim count_cat As String = dtReader.Item("count_")
If count_cat = 0 Then
sql = "insert into tb_category(cat_name) values('" & Me.txtaddress.Text & "')"
Cmd = New SqlCommand(sql, Conn)
Dim sta As String = Cmd.ExecuteNonQuery()
If sta Then
Me.lblText.Text = "บันทึกข้อมูลเรียบร้อย"
Me.lblText.ForeColor = Drawing.Color.Green
GridView1.DataBind()
Else
Me.lblText.Text = "เกิดข้อผิดพลาด"
Me.lblText.ForeColor = Drawing.Color.Red
End If
Else
Me.lblText.Text = "ข้อมูลมีอยู่ในระบบแล้ว กรุณาตรวจสอบ.."
Me.lblText.ForeColor = Drawing.Color.Blue
End If
Catch ex As Exception
Me.Label1.Visible = True
Me.Label1.Text = "Error : " & ex.ToString()
End Try
Else
Me.lblText.Text = "กรุณากรอกข้อมูล"
Me.lblText.ForeColor = Drawing.Color.Red
Me.txtaddress.Focus()
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnString As String
strConnString = "Server=localhost\HUSOC;Uid=user_husoc_ci;PASSWORD=1842531;database=db_husoc_ci;Max Pool Size=400;Connect Timeout=600;"
Conn = New SqlConnection(strConnString)
Conn.Open()
End Sub
End Class
System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.Button1_Click(Object sender, EventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2010\WebSites\WebSite1\Default.aspx.vb:line 23
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim Conn As SqlConnection
Dim Cmd As SqlCommand
Dim Cmd1 As SqlCommand
Dim dtReader As SqlDataReader
Dim sql As String
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If txtaddress.Text <> "" Then
Try
Dim sqlx As String = "select count(cat_id) as count_ from tb_category where cat_name = '" & Me.txtaddress.Text & "'"
Cmd1 = New SqlCommand(sqlx, Conn)
dtReader = Cmd1.ExecuteReader()
dtReader.Read()
Dim count_cat As String = dtReader.Item("count_")
dtReader.Close() 'reader close begin
If count_cat = 0 Then
sql = "insert into tb_category(cat_name) values('" & Me.txtaddress.Text & "')"
Cmd = New SqlCommand(sql, Conn)
Dim sta As String = Cmd.ExecuteNonQuery()
If sta Then
Me.lblText.Text = "บันทึกข้อมูลเรียบร้อย"
Me.lblText.ForeColor = Drawing.Color.Green
GridView1.DataBind()
Else
Me.lblText.Text = "เกิดข้อผิดพลาด"
Me.lblText.ForeColor = Drawing.Color.Red
End If
Else
Me.lblText.Text = "ข้อมูลมีอยู่ในระบบแล้ว กรุณาตรวจสอบ.."
Me.lblText.ForeColor = Drawing.Color.Blue
End If
Catch ex As Exception
Me.Label1.Visible = True
Me.Label1.Text = "Error : " & ex.ToString()
End Try
Else
Me.lblText.Text = "กรุณากรอกข้อมูล"
Me.lblText.ForeColor = Drawing.Color.Red
Me.txtaddress.Focus()
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnString As String
strConnString = "Server=localhost\HUSOC;Uid=user_husoc_ci;PASSWORD=1842531;database=db_husoc_ci;Max Pool Size=400;Connect Timeout=600;"
Conn = New SqlConnection(strConnString)
Conn.Open()
End Sub
End Class