Imports System.Data
Imports System.Data.SqlClient
Partial Class neworeder
Inherits System.Web.UI.Page
Public con As SqlConnection
Public temptable As DataTable
Public da As SqlDataAdapter
Public ds As DataSet
Public editrow As DataRow
Public cmd As SqlCommand
' function for making column of table
Public Function makecart()
Try
temptable = New DataTable("cart")
temptable.Columns.Add("id", GetType(Integer))
temptable.Columns("id").AutoIncrement = True
temptable.Columns("id").AutoIncrementSeed = 1
temptable.Columns.Add("Item_Name", GetType(String))
temptable.Columns.Add("Item_Quantity", GetType(String))
temptable.Columns.Add("Punched_Date", GetType(String))
temptable.Columns.Add("Item_Unit", GetType(String))
temptable.Columns.Add("Comments", GetType(String))
'put table in session
Session("cart") = temptable
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
makecart()
txtdate.Text = Now.Date.ToShortDateString
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
temptable = Session("cart")
Dim imatch As Boolean = False
For Each editrow In temptable.Rows
'checking duplicate item
If editrow("Item_Name") = ddlmeterial.SelectedItem.Text Then
If editrow("Item_Unit") = "Tons" And ddlunit.SelectedItem.Text = "Kg" Then
editrow("Item_Quantity") = (Convert.ToInt32(txtquan.Text) + Convert.ToInt32(editrow("Item_Quantity")) * 1000).ToString
editrow("Item_Unit") = "Kg"
ElseIf editrow("Item_Unit") = "Kg" And ddlunit.SelectedItem.Text = "Tons" Then
editrow("Item_Quantity") = (Convert.ToInt32(txtquan.Text) * 1000 + Convert.ToInt32(editrow("Item_Quantity"))).ToString
editrow("Item_Unit") = "Kg"
ElseIf editrow("Item_Unit") = "Kg" And ddlunit.SelectedItem.Text = "Kg" Then
editrow("Item_Quantity") = (Convert.ToInt32(txtquan.Text) + Convert.ToInt32(editrow("Item_Quantity"))).ToString
ElseIf editrow("Item_Unit") = "Tons" And ddlunit.SelectedItem.Text = "Tons" Then
editrow("Item_Quantity") = (Convert.ToInt32(txtquan.Text) + Convert.ToInt32(editrow("Item_Quantity"))).ToString
End If
imatch = True
End If
Next
If Not imatch Then
editrow = temptable.NewRow
editrow("Item_Name") = ddlmeterial.SelectedItem.Text
editrow("Item_Quantity") = txtquan.Text.Replace("'", "`").ToString
editrow("Punched_Date") = txtdate.Text
editrow("Item_Unit") = ddlunit.SelectedItem.Text
editrow("Comments") = txtcomment.Text
temptable.Rows.Add(editrow)
End If
Session("cart") = temptable
GridView1.DataSource = temptable
GridView1.DataBind()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Try
temptable = Session("cart")
'delete at specific row
temptable.Rows(e.RowIndex).Delete()
Session("cart") = temptable
GridView1.DataSource = temptable
GridView1.DataBind()
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub