Protected Sub btnadd_Click(sender As Object, e As ImageClickEventArgs) Handles btnadd.Click
Dim dt As New DataTable()
Dim ds As New DataSet
Dim dr As DataRow
dt.Columns.Add(New DataColumn("NO.", GetType(Integer)))
dt.Columns("NO.").AutoIncrement = True
dt.Columns("NO.").AutoIncrementSeed = 1
dt.Columns.Add(New DataColumn("ชื่ออาหาร", GetType(String)))
dt.Columns.Add(New DataColumn("ร้านอาหาร", GetType(String)))
dt.Columns.Add(New DataColumn("เพิ่มเติม", GetType(String)))
dt.Columns.Add(New DataColumn("จำนวน/หน่วย", GetType(Integer)))
dt.Columns.Add(New DataColumn("ราคา", GetType(Integer)))
dt.Columns.Add(New DataColumn("ราคารวม", GetType(Integer)))
dr = dt.NewRow()
dr("ชื่ออาหาร") = ddlmenu.SelectedItem.Text.Trim
dr("ร้านอาหาร") = ddlstore2.SelectedItem.Text.Trim
For i = 0 To cboxOption.Items.Count - 1
If cboxOption.Items(i).Selected Then
dr("เพิ่มเติม") = cboxOption.SelectedItem.Text & "," & cboxOption.Items(i).Text
End If
Next
dr("จำนวน/หน่วย") = txtquantity.Text.Trim
dr("ราคา") = txtunitPrice.Text.Trim
dr("ราคารวม") = (txtquantity.Text) * (txtunitPrice.Text)
dt.Rows.Add(dr)
ds.Tables.Add(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Public Function newDataTable()
Dim dt As New DataTable()
Dim ds As New DataSet
Dim dr As DataRow
dt.Columns.Add(New DataColumn("NO.", GetType(Integer)))
dt.Columns("NO.").AutoIncrement = True
dt.Columns("NO.").AutoIncrementSeed = 1
dt.Columns.Add(New DataColumn("ชื่ออาหาร", GetType(String)))
dt.Columns.Add(New DataColumn("ร้านอาหาร", GetType(String)))
dt.Columns.Add(New DataColumn("เพิ่มเติม", GetType(String)))
dt.Columns.Add(New DataColumn("จำนวน/หน่วย", GetType(Integer)))
dt.Columns.Add(New DataColumn("ราคา", GetType(Integer)))
dt.Columns.Add(New DataColumn("ราคารวม", GetType(Integer)))
dr = dt.NewRow()
dr("ชื่ออาหาร") = ddlmenu.SelectedItem.Text.Trim
dr("ร้านอาหาร") = ddlstore2.SelectedItem.Text.Trim
'For i = 0 To cboxOption.Items.Count - 1
' If cboxOption.Items(i).Selected Then
' Dim chk As String
' chk = cboxOption.SelectedItem.Text & "," & cboxOption.Items(i).Text
' 'cboxOption.SelectedItem.Text = cboxOption.SelectedItem.Text & "," & cboxOption.Items(i).Text
' dr("เพิ่มเติม") = chk
' End If
'Next
Dim check As String = ""
For Each li As ListItem In cboxOption.Items
If li.Selected Then
If check = "" Then
check = (li.Text)
dr("เพิ่มเติม") = check
Else
check += ("," + li.Text)
dr("เพิ่มเติม") = check
End If
End If
Next
dr("จำนวน/หน่วย") = txtquantity.Text.Trim
dr("ราคา") = txtunitPrice.Text.Trim
dr("ราคารวม") = (txtquantity.Text) * (txtunitPrice.Text)
dt.Rows.Add(dr)
ds.Tables.Add(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Return dt
End Function
Code (VB.NET)
Protected Sub btnadd_Click(sender As Object, e As ImageClickEventArgs) Handles btnadd.Click
newDataTable()
End Sub
ประวัติการแก้ไข 2015-11-11 13:55:10
Date :
2015-11-11 13:49:16
By :
heaunn
No. 3
Guest
มันจะเพิ่มได้ไงในเมื่อสั่ง new datatable ใหม่ทุกครั้งที add
Public Class WebForm1
Inherits System.Web.UI.Page
Dim dt As New DataTable()
Dim ds As New DataSet
Dim dr As DataRow
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dt.Columns.Add(New DataColumn("NO.", GetType(Integer)))
dt.Columns("NO.").AutoIncrement = True
dt.Columns("NO.").AutoIncrementSeed = 1
dt.Columns.Add(New DataColumn("ชื่ออาหาร", GetType(String)))
dt.Columns.Add(New DataColumn("ร้านอาหาร", GetType(String)))
dt.Columns.Add(New DataColumn("เพิ่มเติม", GetType(String)))
dt.Columns.Add(New DataColumn("จำนวน/หน่วย", GetType(Integer)))
dt.Columns.Add(New DataColumn("ราคา", GetType(Integer)))
dt.Columns.Add(New DataColumn("ราคารวม", GetType(Integer)))
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Public Function newDataTable()
dr = dt.NewRow()
dr("ชื่ออาหาร") = ddlmenu.SelectedItem.Text.Trim
dr("ร้านอาหาร") = ddlstore2.SelectedItem.Text.Trim
'For i = 0 To cboxOption.Items.Count - 1
' If cboxOption.Items(i).Selected Then
' Dim chk As String
' chk = cboxOption.SelectedItem.Text & "," & cboxOption.Items(i).Text
' 'cboxOption.SelectedItem.Text = cboxOption.SelectedItem.Text & "," & cboxOption.Items(i).Text
' dr("เพิ่มเติม") = chk
' End If
'Next
Dim check As String = ""
For Each li As ListItem In cboxOption.Items
If li.Selected Then
If check = "" Then
check = (li.Text)
dr("เพิ่มเติม") = check
Else
check += ("," + li.Text)
dr("เพิ่มเติม") = check
End If
End If
Next
dr("จำนวน/หน่วย") = txtquantity.Text.Trim
dr("ราคา") = txtunitPrice.Text.Trim
dr("ราคารวม") = (txtquantity.Text) * (txtunitPrice.Text)
dt.Rows.Add(dr)
ds.Tables.Add(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Return dt
End Function
End Class
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 239: Next
Line 240: Line 241: dr("จำนวน/หน่วย") = txtquantity.Text.Trim
Line 242: dr("ราคา") = txtunitPrice.Text.Trim
Line 243: dr("ราคารวม") = (txtquantity.Text) * (txtunitPrice.Text)
Dim dt As New DataTable()
Dim ds As New DataSet
Dim dr As DataRow
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 IsPostBack Then
CheckBoxListDataTable()
'txtfoodDetails.ReadOnly = True
loadddlstore()
End If
dt.Columns.Add(New DataColumn("NO.", GetType(Integer)))
dt.Columns("NO.").AutoIncrement = True
dt.Columns("NO.").AutoIncrementSeed = 1
dt.Columns.Add(New DataColumn("ชื่ออาหาร", GetType(String)))
dt.Columns.Add(New DataColumn("ร้านอาหาร", GetType(String)))
dt.Columns.Add(New DataColumn("เพิ่มเติม", GetType(String)))
dt.Columns.Add(New DataColumn("จำนวน", GetType(Integer)))
dt.Columns.Add(New DataColumn("ราคา/หน่วย", GetType(Decimal)))
dt.Columns.Add(New DataColumn("ราคารวม", GetType(Decimal)))
'**** DateToday ***'
txtdate.Text = Date.Today()
End Sub
Sub CheckBoxListDataTable()
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim dt As New DataTable
SqlCon.Open()
Dim strSQL As String
strSQL = "SELECT * FROM OptionFood"
da = New SqlDataAdapter(strSQL, SqlCon)
da.Fill(dt)
da = Nothing
SqlCon.Close()
SqlCon = Nothing
With cboxOption
.DataSource = dt
.DataTextField = "OptionName"
.DataValueField = "rkey"
.DataBind()
End With
'cboxOption.SelectedIndex = cboxOption.Items.IndexOf(cboxOption.Items.FindByValue("1"))
End Sub
Protected Sub loadddlstore()
ddlstore2.AppendDataBoundItems = True
Dim sqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim dt As New DataTable
sqlCon.Open()
Dim strSQL As String
strSQL = "SELECT storeID, storeName FROM Store WHERE online = '1'"
da = New SqlDataAdapter(strSQL, sqlCon)
da.Fill(dt)
da = Nothing
sqlCon.Close()
sqlCon = Nothing
Try
With ddlstore2
.DataSource = dt
.DataTextField = "storeName"
.DataValueField = "storeID"
.DataBind()
.Items.Insert(0, New ListItem("กรุณาเลือกร้านอาหาร", "0"))
End With
ddlstore2.SelectedIndex = ddlstore2.Items.IndexOf(ddlstore2.Items.FindByValue("0"))
Catch ex As Exception
Throw ex
End Try
End Sub
Protected Sub ddlstore2_SelectIndexChanged(sender As Object, e As EventArgs)
ddlstore2.AppendDataBoundItems = True
Dim sqlCon As New SqlConnection(con)
Dim strSQL As String
Dim cmd As New SqlCommand()
strSQL = "SELECT rkey,foodName FROM StoreFood WHERE statusonline='1' AND storeID=@storeID"
cmd.Parameters.AddWithValue("@storeID", ddlstore2.SelectedItem.Value)
cmd.CommandType = CommandType.Text
cmd.CommandText = strSQL
cmd.Connection = sqlCon
Try
sqlCon.Open()
ddlmenu.DataSource = cmd.ExecuteReader()
ddlmenu.DataTextField = "foodName"
ddlmenu.DataValueField = "rkey"
ddlmenu.DataBind()
ddlmenu.Items.Insert(0, New ListItem("กรุณาเลือกรายการอาหาร", "0"))
ddlmenu.SelectedIndex = ddlmenu.Items.IndexOf(ddlmenu.Items.FindByValue("0"))
If ddlmenu.Items.Count > 1 Then
ddlmenu.Enabled = True
Else
ddlmenu.Enabled = False
End If
Catch ex As Exception
Throw ex
Finally
sqlCon.Close()
sqlCon.Dispose()
End Try
End Sub
Protected Sub ddlmenu_SelectIndexChanged(sender As Object, e As EventArgs)
Dim sqlCon As New SqlConnection(con)
Dim strSQL As String
Dim cmd As New SqlCommand()
strSQL = "SELECT foodDetails,foodPic,unitPrice FROM StoreFood WHERE rkey=@rkey"
cmd.Parameters.AddWithValue("@rkey", ddlmenu.SelectedItem.Value)
cmd.CommandType = CommandType.Text
cmd.CommandText = strSQL
cmd.Connection = sqlCon
Try
sqlCon.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Imagefood.Visible = True
Label3.Visible = True
Label3.Text = sdr("foodDetails").ToString()
txtunitPrice.Text = sdr("unitPrice").ToString()
'txtfoodDetails.Visible = True
'txtfoodDetails.Text = sdr(0).ToString()
Imagefood.ImageUrl = sdr("foodPic").ToString()
End While
Catch ex As Exception
Throw ex
Finally
sqlCon.Close()
sqlCon.Dispose()
End Try
End Sub
Protected Sub btnadd_Click(sender As Object, e As ImageClickEventArgs) Handles btnadd.Click
newDataTable()
ResetAll()
End Sub
Public Function newDataTable()
If dt.Columns.Count = 0 Then
dt.Columns.Add("ชื่ออาหาร")
dt.Columns.Add("ร้านอาหาร")
dt.Columns.Add("เพิ่มเติม")
dt.Columns.Add("จำนวน")
dt.Columns.Add("ราคา/หน่วย")
dt.Columns.Add("ราคารวม")
End If
dr = dt.NewRow()
dr("ชื่ออาหาร") = ddlmenu.SelectedItem.Text.Trim
dr("ร้านอาหาร") = ddlstore2.SelectedItem.Text.Trim
'For i = 0 To cboxOption.Items.Count - 1
' If cboxOption.Items(i).Selected Then
' Dim chk As String
' chk = cboxOption.SelectedItem.Text & "," & cboxOption.Items(i).Text
' 'cboxOption.SelectedItem.Text = cboxOption.SelectedItem.Text & "," & cboxOption.Items(i).Text
' dr("เพิ่มเติม") = chk
' End If
'Next
Dim check As String = ""
For Each li As ListItem In cboxOption.Items
If li.Selected Then
If check = "" Then
check = (li.Text)
dr("เพิ่มเติม") = check
Else
check += ("," + li.Text)
dr("เพิ่มเติม") = check
End If
End If
Next
dr("จำนวน") = Integer.Parse(txtquantity.Text.Trim)
dr("ราคา/หน่วย") = Decimal.Parse(txtunitPrice.Text.Trim)
Dim amount As String = Decimal.Parse((txtquantity.Text) * (txtunitPrice.Text))
dr("ราคารวม") = FormatNumber(amount, 2)
dt.Rows.Add(dr)
ds.Tables.Add(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Return dt
End Function
Protected Sub ResetAll()
ddlstore2.SelectedIndex = ddlmenu.Items.IndexOf(ddlmenu.Items.FindByValue("0"))
ddlmenu.SelectedIndex = ddlmenu.Items.IndexOf(ddlmenu.Items.FindByValue("0"))
cboxOption.SelectedIndex = cboxOption.Items.IndexOf(cboxOption.Items.FindByValue("0"))
txtquantity.Text = String.Empty
txtunitPrice.Text = String.Empty
End Sub
End Class
Public Function newDataTable()
If dt.Columns.Count = 0 Then
dt.Columns.Add("ชื่ออาหาร")
dt.Columns.Add("ร้านอาหาร")
dt.Columns.Add("เพิ่มเติม")
dt.Columns.Add("จำนวน")
dt.Columns.Add("ราคา/หน่วย")
dt.Columns.Add("ราคารวม")
End If
Dim check As String = ""
For Each li As ListItem In cboxOption.Items
If li.Selected Then
If check = "" Then
check = (li.Text)
Else
check += ("," + li.Text)
End If
End If
Next
Dim amount As String = Decimal.Parse((txtquantity.Text) * (txtunitPrice.Text))
Dim dr As DataRow = dt.NewRow()
dr("ชื่ออาหาร") = ddlmenu.SelectedItem.Text.Trim
dr("ร้านอาหาร") = ddlstore2.SelectedItem.Text.Trim
dr("เพิ่มเติม") = check
dr("จำนวน") = Integer.Parse(txtquantity.Text.Trim)
dr("ราคา/หน่วย") = Decimal.Parse(txtunitPrice.Text.Trim)
dr("ราคารวม") = FormatNumber(amount, 2)
dt.Rows.Add(dr)
GridView1.DataSource = dt
GridView1.DataBind()
End Function
Public Function newDataTable()
' If dt.Columns.Count = 0 Then
'dt.Columns.Add("ชื่ออาหาร")
' dt.Columns.Add("ร้านอาหาร")
' dt.Columns.Add("เพิ่มเติม")
' dt.Columns.Add("จำนวน")
' dt.Columns.Add("ราคา/หน่วย")
' dt.Columns.Add("ราคารวม")
' End If
' Dim check As String = ""
' For Each li As ListItem In cboxOption.Items
'If li.Selected Then
'If check = "" Then
'check = (li.Text)
'Else
'check += ("," + li.Text)
'End If
'End If
'Next
'Dim amount As String = Decimal.Parse((txtquantity.Text) * (txtunitPrice.Text))
For i As Integer = 0 To 50
Dim dr As DataRow = dt.NewRow()
dr("ชื่ออาหาร") = "ชื่ออาหาร " & i
dr("ร้านอาหาร") = "ร้านอาหาร " & i
dr("เพิ่มเติม") = "เพิ่มเติม " & i
dr("จำนวน") = New Random().Next
dr("ราคา/หน่วย") = New Random().Next
dr("ราคารวม") = New Random().Next
dt.Rows.Add(dr)
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Function
ลองดูว่ามันจะเพิ่มให้ไม๊
ถ้าไม่เพิ่มแสดงว่ามี Function หรือ Event อื่นมีมาเกี่ยวข้องครับ
Dim dt As New DataTable
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 IsPostBack Then
CheckBoxListDataTable()
loadddlstore()
End If
'**** DATE TODAY ***'
txtdate.Text = Date.Today()
End Sub
Sub CheckBoxListDataTable()
Dim SqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim dt As New DataTable
SqlCon.Open()
Dim strSQL As String
strSQL = "SELECT * FROM OptionFood"
da = New SqlDataAdapter(strSQL, SqlCon)
da.Fill(dt)
da = Nothing
SqlCon.Close()
SqlCon = Nothing
With cboxOption
.DataSource = dt
.DataTextField = "OptionName"
.DataValueField = "rkey"
.DataBind()
End With
'cboxOption.SelectedIndex = cboxOption.Items.IndexOf(cboxOption.Items.FindByValue("1"))
End Sub
Protected Sub loadddlstore()
ddlstore2.AppendDataBoundItems = True
Dim sqlCon As New SqlConnection(con)
Dim da As New SqlDataAdapter
Dim dt As New DataTable
Dim strSQL As String
sqlCon.Open()
strSQL = "SELECT storeID, storeName FROM Store WHERE online = '1'"
da = New SqlDataAdapter(strSQL, sqlCon)
da.Fill(dt)
da = Nothing
sqlCon.Close()
sqlCon = Nothing
Try
With ddlstore2
.DataSource = dt
.DataTextField = "storeName"
.DataValueField = "storeID"
.DataBind()
.Items.Insert(0, New ListItem("--- กรุณาเลือกร้านอาหาร ---", "0"))
End With
ddlstore2.SelectedIndex = ddlstore2.Items.IndexOf(ddlstore2.Items.FindByValue("0"))
Catch ex As Exception
Throw ex
End Try
End Sub
Protected Sub ddlstore2_SelectIndexChanged(sender As Object, e As EventArgs)
ddlstore2.AppendDataBoundItems = True
Dim sqlCon As New SqlConnection(con)
Dim strSQL As String
Dim cmd As New SqlCommand()
strSQL = "SELECT rkey,foodName FROM StoreFood WHERE statusonline='1' AND storeID=@storeID"
cmd.Parameters.AddWithValue("@storeID", ddlstore2.SelectedItem.Value)
cmd.CommandType = CommandType.Text
cmd.CommandText = strSQL
cmd.Connection = sqlCon
Try
sqlCon.Open()
ddlmenu.DataSource = cmd.ExecuteReader()
ddlmenu.DataTextField = "foodName"
ddlmenu.DataValueField = "rkey"
ddlmenu.DataBind()
ddlmenu.Items.Insert(0, New ListItem("--- กรุณาเลือกรายการอาหาร ---", "0"))
ddlmenu.SelectedIndex = ddlmenu.Items.IndexOf(ddlmenu.Items.FindByValue("0"))
If ddlmenu.Items.Count > 1 Then
ddlmenu.Enabled = True
Else
ddlmenu.Enabled = False
End If
Catch ex As Exception
Throw ex
Finally
sqlCon.Close()
sqlCon.Dispose()
End Try
End Sub
Protected Sub ddlmenu_SelectIndexChanged(sender As Object, e As EventArgs)
Dim sqlCon As New SqlConnection(con)
Dim strSQL As String
Dim cmd As New SqlCommand()
strSQL = "SELECT foodDetails,foodPic FROM StoreFood WHERE rkey=@rkey"
cmd.Parameters.AddWithValue("@rkey", ddlmenu.SelectedItem.Value)
cmd.CommandType = CommandType.Text
cmd.CommandText = strSQL
cmd.Connection = sqlCon
Try
sqlCon.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
If ddlstore2.Items.Count > 1 Then
Imagefood.Visible = True
txtfoodDetails.Text = sdr(0).ToString()
Imagefood.ImageUrl = sdr("foodPic").ToString()
Else
txtfoodDetails.Text = String.Empty
End If
End While
Catch ex As Exception
Throw ex
Finally
sqlCon.Close()
sqlCon.Dispose()
End Try
End Sub
Protected Sub btnadd_Click(sender As Object, e As ImageClickEventArgs) Handles btnadd.Click
load_Cart()
End Sub
Public Sub Cart()
If dt.TableName.Contains("tblShopCart") = True Then
Exit Sub
Else
dt = New DataTable("tblShopCaet")
dt.Columns.Add(New DataColumn("NO", GetType(Integer)))
dt.Columns("NO").AutoIncrement = True
dt.Columns("NO").AutoIncrementSeed = 1
dt.Columns.Add(New DataColumn("foodName", GetType(String)))
dt.Columns.Add(New DataColumn("store", GetType(String)))
dt.Columns.Add(New DataColumn("option", GetType(String)))
dt.Columns.Add(New DataColumn("QTY", GetType(Integer)))
dt.Columns.Add(New DataColumn("price", GetType(Decimal)))
dt.Columns.Add(New DataColumn("amount", GetType(Decimal)))
End If
Session("ShopCart") = dt
End Sub
Public Sub newDataTable()
Dim check As String = ""
For Each li As ListItem In cboxOption.Items
If li.Selected Then
If check = "" Then
check = (li.Text)
Else
check += ("," + li.Text)
End If
End If
Next
Dim amount As String = Decimal.Parse((txtquantity.Text) * (txtunitPrice.Text))
Dim dt As DataTable = Session("ShopCart")
If dt.Rows.Count <= 0 Then
Dim dr As DataRow = dt.NewRow()
dr("foodName") = ddlmenu.SelectedItem.Text.Trim
dr("store") = ddlstore2.SelectedItem.Text.Trim
dr("option") = check
dr("QTY") = Integer.Parse(txtquantity.Text.Trim)
dr("price") = Decimal.Parse(txtunitPrice.Text.Trim)
dr("amount") = FormatNumber(amount, 2)
dt.Rows.Add(dr)
Session("ShopCart") = dt
Exit Sub
End If
End Sub
Public Sub load_Cart()
If Session("ShopCart") Is Nothing Then
GridView1.DataSource = ""
GridView1.DataBind()
Else
GridView1.DataSource = Session("ShopCart")
GridView1.DataBind()
End If
End Sub
End Class