Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
Dim strconn, strdata, strup As String
Dim num, total, old As Integer
Dim read As SqlDataReader
Dim data As New DataSet
strconn = "Data Source=JERRANAI-PC\SQLEXPRESS;Initial Catalog=DrugStore;Integrated Security=True"
Dim conn As New SqlConnection(strconn)
conn.Open()
strdata = "SELECT DRUG_ID FROM DRUG WHERE DRUG_ID ='" & txt_id.Text & "'"
Dim sqlcomm As New SqlCommand(strdata, conn)
Dim result = sqlcomm.ExecuteNonQuery()
If read.HasRows = True Then
strup = "UPDATE DRUG SET DRUG_TOTAL=@total WHERE DRUG.DRUG_ID ='" & txt_id.Text & "'"
Dim sqlup As New SqlCommand(strup, conn)
Dim view As New SqlDataAdapter(sqlup)
num = txt_total.Text
old = data.Tables("DRUG").Rows(0)(8).ToString
total = old + num
MessageBox.Show(total)
With sqlcomm
.Parameters.Clear()
.CommandType = CommandType.Text
.CommandText = strdata
.Connection = conn
.Parameters.Add("total", SqlDbType.Int).Value = total
read = sqlcomm.ExecuteReader()
If result < 1 Then
MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
txt_name.Text = ""
date_exp.Value = Date.Today
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
Else
MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
date_exp.Value = Date.Now
txt_name.Text = ""
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
End If
End With
Else
Dim strIn = "INSERT INTO DRUG(DRUG_ID,DRUG_NAME,DRUG_PRICE,DRUG_LOW,DRUG_EXP,DRUG_BEXP,DRUG_UNIT,DRUG_TYPE,DRUG_TOTAL)VALUES(@id,@name,@price,@low,@exp,@bexp,@unit,@type,@total)"
Dim sqlIn As New SqlCommand(strIn, conn)
With sqlIn
.Parameters.Clear()
.CommandType = CommandType.Text
.CommandText = strdata
.Connection = conn
.Parameters.Add("id", SqlDbType.NChar).Value = txt_id.Text
.Parameters.Add("name", SqlDbType.NVarChar).Value = txt_name.Text
.Parameters.Add("price", SqlDbType.Money).Value = txt_dprice.Text
.Parameters.Add("low", SqlDbType.Int).Value = txt_low.Text
.Parameters.Add("exp", SqlDbType.Date).Value = date_exp.Text
.Parameters.Add("bexp", SqlDbType.Int).Value = txt_exp.Text
.Parameters.Add("unit", SqlDbType.NVarChar).Value = txt_unit.Text
.Parameters.Add("type", SqlDbType.NVarChar).Value = com_type.Text
.Parameters.Add("total", SqlDbType.Int).Value = txt_total.Text
read = sqlcomm.ExecuteReader()
If result < 1 Then
MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
txt_name.Text = ""
date_exp.Value = Date.Today
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
Else
MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
date_exp.Value = Date.Now
txt_name.Text = ""
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
End If
End With
End If
conn.Close()
End Sub
Private Sub tbx_dbexp_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_exp.TextChanged
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strConn, strData As String
strConn = "Data Source=JERRANAI-PC\SQLEXPRESS;Initial Catalog=DrugStore;Integrated Security=True"
Dim conn As New SqlConnection(strConn)
conn.Open()
strData = "SELECT DRUG_ID FROM DRUG WHERE DRUG_ID ='" & txt_id.Text & "'"
Dim sqlcomm As New SqlCommand(strData, conn)
Dim read As SqlDataReader
read = sqlcomm.ExecuteReader
If read.HasRows = True Then
Dim num, total, old As Integer
strData = "UPDATE DRUG SET DRUG_TOTAL=@total WHERE DRUG.DRUG_ID ='" & txt_id.Text & "'"
Dim strcomm As New SqlCommand(strData, conn)
Dim view As New SqlDataAdapter(sqlcomm)
Dim data As New DataSet
num = txt_total.Text
old = data.Tables("DRUG").Rows(0)(8).ToString
total = old + num
With sqlcomm
.Parameters.Clear()
.CommandType = CommandType.Text
.CommandText = strData
.Connection = conn
.Parameters.Add("total", SqlDbType.Int).Value = total
MessageBox.Show(total)
End With
Dim result = sqlcomm.ExecuteNonQuery()
If result < 1 Then
MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
txt_name.Text = ""
date_exp.Value = Date.Today
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
Else
MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
date_exp.Value = Date.Now
txt_name.Text = ""
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
End If
read.Close()
conn.Close()
Else
strData = "INSERT INTO DRUG(DRUG_ID,DRUG_NAME,DRUG_PRICE,DRUG_LOW,DRUG_EXP,DRUG_BEXP,DRUG_UNIT,DRUG_TYPE,DRUG_TOTAL)VALUES(@id,@name,@price,@low,@exp,@bexp,@unit,@type,@total)"
Dim strcomm As New SqlCommand(strData, conn)
With strcomm
.Parameters.Clear()
.CommandType = CommandType.Text
.CommandText = strData
.Connection = conn
.Parameters.Add("id", SqlDbType.NChar).Value = txt_id.Text
.Parameters.Add("name", SqlDbType.NVarChar).Value = txt_name.Text
.Parameters.Add("price", SqlDbType.Money).Value = txt_dprice.Text
.Parameters.Add("low", SqlDbType.Int).Value = txt_low.Text
.Parameters.Add("exp", SqlDbType.Date).Value = date_exp.Text
.Parameters.Add("bexp", SqlDbType.Int).Value = txt_exp.Text
.Parameters.Add("unit", SqlDbType.NVarChar).Value = txt_unit.Text
.Parameters.Add("type", SqlDbType.NVarChar).Value = com_type.Text
.Parameters.Add("total", SqlDbType.Int).Value = txt_total.Text
Dim result = strcomm.ExecuteNonQuery()
If result < 1 Then
MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
txt_name.Text = ""
date_exp.Value = Date.Today
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
Else
MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
date_exp.Value = Date.Now
txt_name.Text = ""
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
End If
End With
End If
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, VB.NET, VS 2010 (.NET 4.x)
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strConn, strData As String
strConn = "Data Source=JERRANAI-PC\SQLEXPRESS;Initial Catalog=DrugStore;Integrated Security=True"
Dim conn As New SqlConnection(strConn)
conn.Open()
strData = "SELECT DRUG_ID FROM DRUG WHERE DRUG_ID ='" & txt_id.Text & "'"
Dim sqlcomm As New SqlCommand(strData, conn)
Dim read As SqlDataReader
read = sqlcomm.ExecuteReader
If read.HasRows = True Then
Dim num, total, old As Integer
strData = "UPDATE DRUG SET DRUG_TOTAL=@total WHERE DRUG.DRUG_ID ='" & txt_id.Text & "'"
Dim strcomm As New SqlCommand(strData, conn)
Dim view As New SqlDataAdapter(sqlcomm)
Dim data As New DataSet
num = txt_total.Text
old = data.Tables("DRUG").Rows(0)(8).ToString
total = old + num
With sqlcomm
.Parameters.Clear()
.CommandType = CommandType.Text
.CommandText = strData
.Connection = conn
.Parameters.Add("total", SqlDbType.Int).Value = total
MessageBox.Show(total)
End With
Dim result = sqlcomm.ExecuteNonQuery()
If result < 1 Then
MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
txt_name.Text = ""
date_exp.Value = Date.Today
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
Else
MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
date_exp.Value = Date.Now
txt_name.Text = ""
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
End If
read.Close()
conn.Close()
Else
strData = "INSERT INTO DRUG(DRUG_ID,DRUG_NAME,DRUG_PRICE,DRUG_LOW,DRUG_EXP,DRUG_BEXP,DRUG_UNIT,DRUG_TYPE,DRUG_TOTAL)VALUES(@id,@name,@price,@low,@exp,@bexp,@unit,@type,@total)"
Dim strcomm As New SqlCommand(strData, conn)
With strcomm
.Parameters.Clear()
.CommandType = CommandType.Text
.CommandText = strData
.Connection = conn
.Parameters.Add("id", SqlDbType.NChar).Value = txt_id.Text
.Parameters.Add("name", SqlDbType.NVarChar).Value = txt_name.Text
.Parameters.Add("price", SqlDbType.Money).Value = txt_dprice.Text
.Parameters.Add("low", SqlDbType.Int).Value = txt_low.Text
.Parameters.Add("exp", SqlDbType.Date).Value = date_exp.Text
.Parameters.Add("bexp", SqlDbType.Int).Value = txt_exp.Text
.Parameters.Add("unit", SqlDbType.NVarChar).Value = txt_unit.Text
.Parameters.Add("type", SqlDbType.NVarChar).Value = com_type.Text
.Parameters.Add("total", SqlDbType.Int).Value = txt_total.Text
Dim result = strcomm.ExecuteNonQuery()
If result < 1 Then
MessageBox.Show("บันทึกข้อมูลผิดพลาด", "ล้มเหลว", MessageBoxButtons.OK, MessageBoxIcon.Error)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
txt_name.Text = ""
date_exp.Value = Date.Today
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
Else
MessageBox.Show("บันทึกเรียบร้อย", "เรียบร้อย", MessageBoxButtons.OK, MessageBoxIcon.Information)
txt_id.Text = ""
txt_dprice.Text = ""
txt_exp.Text = ""
txt_low.Text = ""
date_exp.Value = Date.Now
txt_name.Text = ""
txt_total.Text = ""
txt_unit.Text = ""
com_type.Text = ""
txt_id.Focus()
End If
End With
End If
End Sub