Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Imports System.Drawing
Imports System.Web.UI.WebControls.Image
Public Class Food1
Inherits System.Web.UI.Page
Dim con As String = ConfigurationManager.ConnectionStrings("con").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DropdownlistStore()
buttonSave.Attributes.Add("onclick", "return confirm('คุณต้องการบันทึกใช่หรือไม่?');")
loadStoreFood()
End If
End Sub
Protected Sub loadStoreFood()
Dim SqlCon As New SqlConnection(con)
SqlCon.Open()
Dim cmd As New SqlCommand("SELECT rkey,storeID,foodName,foodDetails,unitPrice,statusonline,foodPic FROM StoreFood", SqlCon)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
Dim count As Integer = ds.Tables(0).Rows.Count
SqlCon.Close()
If ds.Tables(0).Rows.Count > 0 Then
gridStoreFood.DataSource = ds
gridStoreFood.DataBind()
Else
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow())
gridStoreFood.DataSource = ds
gridStoreFood.DataBind()
Dim columcount As Integer = gridStoreFood.Rows(0).Cells.Count
End If
End Sub
'*** DROPDOWNLIST STORES ***'
Function DropdownlistStore() As DataTable
Dim strSQL As String
Dim da As SqlDataAdapter
Dim dt As New DataTable
Dim sqlCon As New SqlConnection(con)
sqlCon.Open()
strSQL = "SELECT storeID,storeName FROM Store"
da = New SqlDataAdapter(strSQL, sqlCon)
da.Fill(dt)
da = Nothing
sqlCon.Close()
With ddlstore1
.DataSource = dt
.DataTextField = "storeName"
.DataValueField = "storeID"
.DataBind()
.Items.Insert(0, New ListItem("--- กรุณาเลือกร้านอาหาร ---", "0"))
End With
ddlstore1.SelectedIndex = ddlstore1.Items.IndexOf(ddlstore1.Items.FindByValue("0"))
Return dt
End Function
Protected Sub buttonSave_click(sender As Object, e As ImageClickEventArgs) Handles buttonSave.Click
'*** UPLOAD PICTURE ***'
If fiUpload.HasFile Then
Dim intWidth, intHeight As Integer
Dim UlFileName As String
intWidth = 120 '*** Fix Width ***'
'intHright = 0 '*** If = 0 Auto Re-cal Size
intHeight = 120
UlFileName = "Image_Food/" & fiUpload.FileName
'*** SAVE IMAGE ***'
fiUpload.SaveAs(Server.MapPath(UlFileName))
Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(UlFileName))
Dim objBitmap As Bitmap
'*** CALCULATE HEIGHT ***'
If intHeight > 0 Then
objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
Else
If objGraphic.Width > intWidth Then
Dim ratio As Double = objGraphic.Height / objGraphic.Width
intHeight = ratio * intWidth
objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
Else
objBitmap = New Bitmap(objGraphic)
End If
End If
objGraphic.Dispose()
objBitmap = Nothing
objGraphic = Nothing
'*** INSERT ***'
Dim SqlCon As New SqlConnection(con)
Dim cmd As New SqlCommand()
SqlCon.Open()
cmd.CommandText = "INSERT INTO StoreFood (storeID,foodName,foodDetails,unitPrice,statusonline,foodPic) VALUES ('" & ddlstore1.SelectedItem.Value & "', '" & txtfoodName.Text.Trim & "', '" & txtfoodDetails.Text & "', '" & txtunitPrice.Text & "' , '" & rdbonline.SelectedValue & "' , '" & UlFileName & "')"
cmd.Connection = SqlCon
'*** NEW ROWS ***'
Dim result As Integer = cmd.ExecuteNonQuery()
Label12.Text = cmd.CommandText
SqlCon.Close()
If result = 1 Then
loadStoreFood()
End If
SqlCon = Nothing
ResetAll()
End If
End Sub
Protected Sub btncancel_Click(sender As Object, e As ImageClickEventArgs) Handles btncancel1.Click
ResetAll()
End Sub
'*** IMAGEBUTTON EDIT ***'
Protected Sub gridStoreFood_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gridStoreFood.RowEditing
gridStoreFood.EditIndex = e.NewEditIndex
loadStoreFood()
End Sub
Protected Sub gridStoreFood_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles gridStoreFood.RowCancelingEdit
gridStoreFood.EditIndex = -1
loadStoreFood()
End Sub
Sub ShowPageCommand(sender As Object, e As GridViewPageEventArgs) Handles gridStoreFood.PageIndexChanging
gridStoreFood.PageIndex = e.NewPageIndex
loadStoreFood()
End Sub
'*** IMAGEBUTTON DELETE ***'
Protected Sub gridStoreFood_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Handles gridStoreFood.RowDeleting
Dim SqlCon As New SqlConnection(con)
Dim rkey As String = gridStoreFood.DataKeys(e.RowIndex).Values("rkey").ToString()
Dim cmd As New SqlCommand("DELETE FROM StoreFood WHERE rkey = '" + rkey + "'", SqlCon)
SqlCon.Open()
cmd.ExecuteNonQuery()
gridStoreFood.EditIndex = -1
SqlCon.Close()
loadStoreFood()
End Sub
Protected Sub gridStoreFood_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridStoreFood.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlstore1 As DropDownList = CType(e.Row.FindControl("ddlstore1"), DropDownList)
If Not IsNothing(ddlstore1) Then
ddlstore1.DataSource = DropdownlistStore()
ddlstore1.DataTextField = "storeName"
ddlstore1.DataValueField = "storeID"
ddlstore1.DataBind()
'ddlstore1.Items.Insert(0, New ListItem("เลือกร้านอาหาร", "0"))
ddlstore1.SelectedValue = ddlstore1.Items.IndexOf(ddlstore1.Items.FindByValue(e.Row.DataItem("storeID")))
End If
Dim lblfoodName As Label = CType(e.Row.FindControl("lblfoodName"), Label)
If Not IsNothing(lblfoodName) Then
lblfoodName.Text = e.Row.DataItem("foodName")
End If
Dim lblfoodDetails As Label = CType(e.Row.FindControl("lblfoodDetails"), Label)
If Not IsNothing(lblfoodDetails) Then
lblfoodDetails.Text = e.Row.DataItem("foodDetails")
End If
Dim lblunitPrice As Label = CType(e.Row.FindControl("lblunitPrice"), Label)
If Not IsNothing(lblunitPrice) Then
lblunitPrice.Text = e.Row.DataItem("unitPrice")
End If
Dim lblstatusonline As Label = CType(e.Row.FindControl("lblstatusonline"), Label)
If Not IsNothing(lblstatusonline) Then
If e.Row.DataItem("statusonline") = "0" Then
lblstatusonline.Text = "ปิดร้าน"
Else
lblstatusonline.Text = "เปิดร้าน"
End If
End If
End If
End Sub
Protected Sub gridStoreFood_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles gridStoreFood.RowUpdating
'Dim storeID As TextBox = CType(gridStore.Rows(e.RowIndex).FindControl("txtstoreID"), TextBox)
Dim rkey As String = gridStoreFood.DataKeys(e.RowIndex).Values("rkey").ToString()
Dim ddlstore1 As DropDownList = CType(gridStoreFood.Rows(e.RowIndex).FindControl("ddlstore1"), DropDownList)
Dim foodName As TextBox = CType(gridStoreFood.Rows(e.RowIndex).FindControl("txtfoodName"), TextBox)
Dim foodDetails As TextBox = CType(gridStoreFood.Rows(e.RowIndex).FindControl("txtfoodDetails"), TextBox)
Dim unitPrice As TextBox = CType(gridStoreFood.Rows(e.RowIndex).FindControl("txtunitPrice"), TextBox)
Dim statusonline As RadioButtonList = CType(gridStoreFood.Rows(e.RowIndex).FindControl("rdbonline"), RadioButtonList)
Dim fiUpload As FileUpload = CType(gridStoreFood.Rows(e.RowIndex).FindControl("fiUpload"), FileUpload)
Dim UlFileName As String = "Image_Food/" & fiUpload.FileName
'Dim strUpdateFile = String.Empty
If fiUpload.HasFile Then
UlFileName += fiUpload.FileName
fiUpload.SaveAs(Server.MapPath(UlFileName))
'strUpdateFile = "storePic='" & UlFileName & "' "
Else
Dim img As System.Web.UI.WebControls.Image = CType(gridStoreFood.Rows(e.RowIndex).FindControl("imageUser"), System.Web.UI.WebControls.Image)
UlFileName = img.ImageUrl
End If
Dim SqlCon As New SqlConnection(con)
Dim cmd As New SqlCommand("UPDATE StoreFood SET storeID='" & ddlstore1.SelectedItem.Value & "', foodName='" & foodName.Text & "', foodDetails='" & foodDetails.Text & "', unitPrice='" & unitPrice.Text & "', statusonline='" & statusonline.SelectedValue & "', foodPic='" & UlFileName & "' WHERE rkey='" & gridStoreFood.DataKeys.Item(e.RowIndex).Value & "'", SqlCon)
SqlCon.Open()
cmd.ExecuteNonQuery()
SqlCon.Close()
gridStoreFood.EditIndex = -1
loadStoreFood()
End Sub
Sub checknull()
End Sub
'*** BUTTON CANCEL ***'
Protected Sub ResetAll()
ddlstore1.SelectedIndex = ddlstore1.Items.IndexOf(ddlstore1.Items.FindByValue("0"))
txtfoodName.Text = ""
txtfoodDetails.Text = ""
txtunitPrice.Text = ""
rdbonline.SelectedIndex = rdbonline.Items.IndexOf(rdbonline.Items.FindByValue("1"))
End Sub
End Class