|
|
|
เราจะเช็คค่าว่างใน DataTable ได้อย่างไรครับ แนะนำด้วยครับ |
|
|
|
|
|
|
|
ผมจะเพิ่มข้อมูลลง dt (DataTable) ก่อนแล้วจึงจะบันทึกลง db (DataBase) ผมอยากเช็คว่าถ้า dt ไม่มีข้อมูล ให้ไปเพิ่มข้อมูล dt ก่อน
Code เพิ่มข้อมูลลง dt
Code (VB.NET)
Private Sub LoadSizeData()
If IsNothing(Session("SizeTable")) Then
dt = New DataTable()
dt.Columns.Add(New DataColumn("TopWidth", GetType(String)))
dt.Columns.Add(New DataColumn("MidWidth", GetType(String)))
dt.Columns.Add(New DataColumn("BottomWidth", GetType(String)))
dt.Columns.Add(New DataColumn("WidthAVG", GetType(String)))
dt.Columns.Add(New DataColumn("LeftLength", GetType(String)))
dt.Columns.Add(New DataColumn("RigthLength", GetType(String)))
dt.Columns.Add(New DataColumn("LengthAVG", GetType(String)))
dt.Columns.Add(New DataColumn("SizeNote", GetType(String)))
Session("SizeTable") = dt
End If
dt = DirectCast(Session("SizeTable"), DataTable)
gvS_LoadSize.DataSource = dt
gvS_LoadSize.DataBind()
End Sub
Private Sub AddToTable()
If txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
Dim dr As DataRow = dt.NewRow
dr("TopWidth") = txtPK_WidthTop.Text.Trim
dr("MidWidth") = txtPK_WidthMid.Text.Trim
dr("BottomWidth") = txtPK_WidthBottom.Text.Trim
'dr("WidthAVG") = ((CType(txtPK_WidthTop.Text.Trim, Integer) + CType(txtPK_WidthMid.Text.Trim, Integer) + CType(txtPK_WidthBottom.Text.Trim, Integer)) / 3).ToString("#,##0.00")
'dr("WidthAVG") = ((CType(txtPK_WidthTop.Text.Trim, Integer) + CType(txtPK_WidthMid.Text.Trim, Integer) + CType(txtPK_WidthBottom.Text.Trim, Integer)) / 3).ToString("#0.00")
dr("WidthAVG") = ((CType(txtPK_WidthTop.Text.Trim, Decimal) + CType(txtPK_WidthMid.Text.Trim, Decimal) + CType(txtPK_WidthBottom.Text.Trim, Decimal)) / 3).ToString("n2")
dr("LeftLength") = txtPK_LengthLeft.Text.Trim
dr("RigthLength") = txtPK_LengthRigth.Text.Trim
'dr("LengthAVG") = ((CType(txtPK_LengthLeft.Text.Trim, Integer) + CType(txtPK_LengthRigth.Text.Trim, Integer)) / 2).ToString("#,##0.00")
'dr("LengthAVG") = ((CType(txtPK_LengthLeft.Text.Trim, Integer) + CType(txtPK_LengthRigth.Text.Trim, Integer)) / 2).ToString("#0.00")
dr("LengthAVG") = ((CType(txtPK_LengthLeft.Text.Trim, Decimal) + CType(txtPK_LengthRigth.Text.Trim, Decimal)) / 2).ToString("n2")
dr("SizeNote") = txtPK_SizeNote.Text.Trim
dt.Rows.Add(dr)
Session("SizeTable") = dt
LoadSizeData()
Reset()
txtPK_WidthTop.Focus()
ElseIf txtPK_WidthTop.Text.Trim = String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
MsgBox("กรุณากรอกความกว้างส่วนบน")
txtPK_WidthTop.Focus()
ElseIf txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim = String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
MsgBox("กรุณากรอกความกว้างส่วนกลาง")
txtPK_WidthMid.Focus()
ElseIf txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim = String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
MsgBox("กรุณากรอกความกว้างส่วนล่าง")
txtPK_WidthBottom.Focus()
ElseIf txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim = String.Empty And txtPK_LengthRigth.Text.Trim <> String.Empty Then
MsgBox("กรุณากรอกความยาวด้านซ้าย")
txtPK_LengthLeft.Focus()
ElseIf txtPK_WidthTop.Text.Trim <> String.Empty And txtPK_WidthMid.Text.Trim <> String.Empty And txtPK_WidthBottom.Text.Trim <> String.Empty And txtPK_LengthLeft.Text.Trim <> String.Empty And txtPK_LengthRigth.Text.Trim = String.Empty Then
MsgBox("กรุณากรอกความยาวด้านขวา")
txtPK_LengthRigth.Focus()
Else
MsgBox("กรุณากรอกข้อมูลให้ครบถ้วน")
txtPK_WidthTop.Focus()
End If
End Sub
Protected Sub btnPK_Add_Click(sender As Object, e As EventArgs) Handles btnPK_Add.Click
AddToTable()
End Sub
Code บันทึกลง db
Code (VB.NET)
Private Sub AddTODB()
Try
ConnectDB()
objConn.Open()
strSQL = "SELECT ISNULL(MAX(RIGHT(PK_ID,2)),0) As PK_ID FROM Package"
objCmd = New SqlCommand(strSQL, objConn)
Count = objCmd.ExecuteScalar()
iMAX = IIf(Count <= 0, 1, Count + 1)
If iMAX < 100 Then
strSQL = "INSERT INTO Package (PK_ID, PK_RECEIVEDATE) VALUES ('" & "PK" & CStr(Format(Now.Date.ToString("yyyyMMdd", New CultureInfo("en-US")))) & iMAX.ToString("00") & "',@PK_RECEIVEDATE)"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@PK_RECEIVEDATE", SqlDbType.Date).Value = Format(Now.Date.ToString("dd-MM-yyyy", New CultureInfo("en-US")))
objCmd.ExecuteNonQuery()
iMAX = IIf(Count <= 0, 1, Count + 1)
If iMAX < 100 Then
dt = DirectCast(Session("SizeTable"), DataTable)
For i = 0 To dt.Rows.Count - 1
strSQL = "INSERT INTO Size (PK_ID,S_WIDTHTOP,S_WIDTHMID,S_WIDTHBOTTOM,S_LENGTHLEFT,S_LENGTHRIGTH,S_NOTE) VALUES ('" & "PK" & CStr(Format(Now.Date.ToString("yyyyMMdd", New CultureInfo("en-US")))) & iMAX.ToString("00") & "','" & dt.Rows(i)("TopWidth") & "','" & dt.Rows(i)("MidWidth") & "','" & dt.Rows(i)("BottomWidth") & "','" & dt.Rows(i)("LeftLength") & "','" & dt.Rows(i)("RigthLength") & "','" & dt.Rows(i)("SizeNote") & "')"
objCmd = New SqlCommand(strSQL, objConn)
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
Next
objConn.Close()
objConn = Nothing
MsgBox("บันทึกข้อมูลสำเร็จ")
dt.Clear()
Response.Redirect("~/Test/Test_Manage_Package.aspx")
End If
Else
MsgBox("ข้อมูลเต็ม")
End If
Catch ex As Exception
Response.Write("Error : " + ex.Message)
End Try
End Sub
Protected Sub btnPK_Save_Click(sender As Object, e As EventArgs) Handles btnPK_Save.Click
AddTODB()
End Sub
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET, VS 2012 (.NET 4.x)
|
ประวัติการแก้ไข 2017-02-10 15:30:00
|
|
|
|
|
Date :
2017-02-10 15:29:05 |
By :
NUTKA |
View :
1469 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้และครับ
dt.rows.count > 0
|
|
|
|
|
Date :
2017-02-10 16:06:39 |
By :
NUTKA |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2017-02-10 16:59:38 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2017-02-10 17:16:30 |
By :
lamaka.tor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|