Copyright © 2003-2009
By Suthep Sangvirotjanaphat
  Mobile: 089-967-2200, 081-915-7816
Phone: 0-2992-4877   Fax: 0-2992-4878
Fast Contact Us
^
%
.

GreatFriends.Biz Community

Custom Search
 MESSAGE #102560 (อ่าน 997 ครั้ง)

รบกวนปรึกษา เรื่อง Objectdatasource ในการ insert เข้า database ครับ (งง มากๆ)

Tags: Web, VB, SQL Server 2005, VS 2008, Article

คือ ทั้งหมด ผมสามารถส่ง เข้า database ได้ปกติ ครับ  แต่ .....

ถ้าผมกรอกไม่ครบ ทุก ฟิลด์ (textbox) ในหน้า *.aspx  ก็จะไม่สามารถ insert ได้ครับ
 
ผมเขียนเป็น OOP ครับ
มี DAL กับ BLL
ในBLL ผมจะ สร้าง class เอาไว้กำหนด Property ของ ฟิลด์ ทุก ฟิลด์ ใน database  พร้อมทั้งกำหนด ค่า default ไว้ ดังนี้ครับ
 
 

Imports Microsoft.VisualBasic

Public Class Customer

Private _id As Integer = 0

Private _custypeid As Integer = 0

Private _cusstatus As Boolean = True

Private _cuscode As String = Customer_CodeGenerate.cuscode_create(6)

Private _sex As Boolean = False

Private _firstname As String = ""

Private _lastname As String = ""

Private _nick As String = ""

Private _birthday As DateTime = DateTime.MaxValue

Private _address As String = ""

Private _datesbm As DateTime = DateTime.Now

Private _cusPhone As String = ""

Private _cusmoblie As String = ""

Private _cusemail As String = ""

Private _avatar As String = ""

Private _height As String = ""

Private _weight As String = ""

Private _condition As Boolean = True

Private _bone As Boolean = True

Private _goal As String = ""

Private _cusnote As String = "null"

 

Public Property ID() As Integer

Get

Return _id

End Get

Set(ByVal value As Integer)

_id = value

End Set

End Property

Public Property CustypeID() As Integer

Get

Return _custypeid

End Get

Set(ByVal value As Integer)

_custypeid = value

End Set

End Property

Public Property CusCode() As String

Get

Return _cuscode

End Get

Set(ByVal value As String)

_cuscode = value

End Set

End Property

Public Property Cusstatus() As Boolean

Get

Return _cusstatus

End Get

Set(ByVal value As Boolean)

_cusstatus = value

End Set

End Property

Public Property CUSSEX() As Boolean

Get

Return _sex

End Get

Set(ByVal value As Boolean)

_sex = value

End Set

End Property

Public Property Firstname() As String

Get

Return _firstname

End Get

Set(ByVal value As String)

_firstname = value

End Set

End Property

Public Property Lastname() As String

Get

Return _lastname

End Get

Set(ByVal value As String)

_lastname = value

End Set

End Property

Public Property Nickname() As String

Get

Return _nick

End Get

Set(ByVal value As String)

_nick = value

End Set

End Property

Public Property BD() As DateTime

Get

Return _birthday

End Get

Set(ByVal value As DateTime)

_birthday = value

End Set

End Property

Public Property Address() As String

Get

Return _address

End Get

Set(ByVal value As String)

_address = value

End Set

End Property

Public Property DateSubmit() As DateTime

Get

Return _datesbm

End Get

Set(ByVal value As DateTime)

_datesbm = value

End Set

End Property

Public Property Phone() As String

Get

Return _cusPhone

End Get

Set(ByVal value As String)

_cusPhone = value

End Set

End Property

Public Property Moblie() As String

Get

Return _cusmoblie

End Get

Set(ByVal value As String)

_cusmoblie = value

End Set

End Property

Public Property EMail() As String

Get

Return _cusemail

End Get

Set(ByVal value As String)

_cusemail = value

End Set

End Property

Public Property Avatar() As String

Get

Return _avatar

End Get

Set(ByVal value As String)

_avatar = value

End Set

End Property

Public Property Height() As String

Get

Return _height

End Get

Set(ByVal value As String)

_height = value

End Set

End Property

Public Property Weight() As String

Get

Return _weight

End Get

Set(ByVal value As String)

_weight = value

End Set

End Property

Public Property Condition() As Boolean

Get

Return _condition

End Get

Set(ByVal value As Boolean)

_condition = value

End Set

End Property

Public Property Bone() As Boolean

Get

Return _bone

End Get

Set(ByVal value As Boolean)

_bone = value

End Set

End Property

Public Property Goal() As String

Get

Return _goal

End Get

Set(ByVal value As String)

_goal = value

End Set

End Property

Public Property Cusnote() As String

Get

Return _cusnote

End Get

Set(ByVal value As String)

_cusnote = value

End Set

End Property

Public Sub New()

End Sub

Public Sub New(ByVal id As Integer, ByVal custypeid As Integer, ByVal cusstatus As Boolean, ByVal cuscode As String, ByVal cussex As Boolean, ByVal firstname As String, ByVal lastname As String, ByVal nick As String, ByVal birthday As DateTime, ByVal address As String, ByVal datesbm As DateTime, ByVal cusphone As String, ByVal cusmobile As String, ByVal cusemail As String, ByVal avatar As String, ByVal height As String, ByVal weight As String, ByVal condition As Boolean, ByVal bone As Boolean, ByVal goal As String, ByVal Cusnote As String)

Me.ID = id

Me.CustypeID = custypeid

Me.Cusstatus = cusstatus

Me.CusCode = cuscode

Me.CUSSEX = cussex

Me.Firstname = firstname

Me.Lastname = lastname

Me.Nickname = nick

Me.BD = birthday

Me.Address = address

Me.DateSubmit = datesbm

Me.Phone = cusphone

Me.Moblie = cusmobile

Me.EMail = cusemail

Me.Avatar = avatar

Me.Height = height

Me.Weight = weight

Me.Condition = condition

Me.Bone = bone

Me.Goal = goal

Me.Cusnote = Cusnote

End Sub

End Class

--------------

แล้วก็จะมี class ใน DAL 

คือ เอาไว้อ้างถึง class ด้านบนนี้น่ะครับ

 CutomerDB CLass ----------

Public Shared Sub InsertCustomer(ByVal Thecustomer As Customer)

Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)

Dim myCommand As SqlCommand = New SqlCommand("Insert Into tbl_customer(cus_code, cus_status, cus_sex, cus_first_name, cus_last_name, cus_nick_name, cus_BD, cus_Add, cus_submit, cus_type_id, cus_phone, cus_mobile, cus_email, cus_avatar_path, cus_height, cus_weight, cus_medical_condition, cus_bone_acc, cus_goal, cus_note)Values(@cus_code, @cus_status, @cus_sex, @cus_first_name, @cus_last_name, @cus_nick_name, @cus_BD, @cus_Add, @cus_submit, @cus_type_id, @cus_phone, @cus_mobile, @cus_email, @cus_avatar, @cus_height, @cus_weight, @cusmedical_condition, @cus_bone_acc, @cus_goal, @cus_note)", myConnection) '

myConnection.Open()

myCommand.Parameters.Add("@id", SqlDbType.Int).Value = Thecustomer.ID

myCommand.Parameters.Add("@cus_code", SqlDbType.NVarChar).Value = Thecustomer.CusCode

myCommand.Parameters.Add("@cus_status", SqlDbType.Bit).Value = Thecustomer.Cusstatus

myCommand.Parameters.Add("@cus_sex", SqlDbType.Bit).Value = Thecustomer.CUSSEX

myCommand.Parameters.Add("@cus_first_name", SqlDbType.NVarChar).Value = Thecustomer.Firstname

myCommand.Parameters.Add("@cus_last_name", SqlDbType.NVarChar).Value = Thecustomer.Lastname

myCommand.Parameters.Add("@cus_nick_name", SqlDbType.NVarChar).Value = Thecustomer.Nickname

myCommand.Parameters.Add("@cus_BD", SqlDbType.DateTime).Value = Thecustomer.BD

myCommand.Parameters.Add("@cus_Add", SqlDbType.NVarChar).Value = Thecustomer.Address

myCommand.Parameters.Add("@cus_submit", SqlDbType.NVarChar).Value = Thecustomer.DateSubmit

myCommand.Parameters.Add("@cus_type_id", SqlDbType.Int).Value = Thecustomer.CustypeID

myCommand.Parameters.Add("@cus_phone", SqlDbType.NChar).Value = Thecustomer.Phone

myCommand.Parameters.Add("@cus_mobile", SqlDbType.NChar).Value = Thecustomer.Moblie

myCommand.Parameters.Add("@cus_email", SqlDbType.NVarChar).Value = Thecustomer.EMail

myCommand.Parameters.Add("@cus_avatar", SqlDbType.VarChar).Value = Thecustomer.Avatar

myCommand.Parameters.Add("@cus_height", SqlDbType.NVarChar).Value = Thecustomer.Height

myCommand.Parameters.Add("@cus_weight", SqlDbType.NVarChar).Value = Thecustomer.Weight

myCommand.Parameters.Add("@cusmedical_condition", SqlDbType.Bit).Value = Thecustomer.Condition

myCommand.Parameters.Add("@cus_bone_acc", SqlDbType.Bit).Value = Thecustomer.Bone

myCommand.Parameters.Add("@cus_goal", SqlDbType.NVarChar).Value = Thecustomer.Goal

myCommand.Parameters.Add("@cus_note", SqlDbType.NVarChar).Value = Thecustomer.Cusnote

myCommand.ExecuteNonQuery()

myConnection.Close()

End Using

End Sub

 

แล้วสุดท้าย  จะมี calss ใน ฝั่ง BLL

เอาไว้จัดการ แต่ในกรณีนี้ แค่ insert ไม่มีเงื่อนไขใดๆ ก็เรียกตรงๆ

customermanage  CLass---

Public Shared Sub InsertNewcustomer(ByVal thecustomer As Customer)

CustomerDB.InsertCustomer(thecustomer)

End Sub

 

---- ทังหมดนีก็ทำงานได้ปกติครับ

ในกรณี ที่ผมกรอก ทุก อัน หมด แต่ถ้ากรอกไม่ครบ ก็จะ error 

ซึ่งใน ดาต้าเบส ผมกำหนด ได้แค่บางอันเท่านั้น ที่ เป็ฯค่า Null ได้ ซึ่งในกรณีนี้ อันที่ผม เซ้ต เป็นค่า NULL ก็ต้องผ่าน แต่เท่าที่ผมเข้าใจถ้าเขียนแบบนี้
 
method  insert  ของผม เรียกใช้ class customer ที่มีค่า default อยู่แล้ว ซึ่งจะไม่มีค่า null เลย ในแต่ละ record ใหม่ ที่เกิดขึ้น
 
ปัญหาที่เกิดขึ้น ในหน้า form ที่ให้ user กรอก ผมจะมีดอกจันทร์ไว้ว่า อันนี้ต้องกรอกนะ นอกนั้น กรอกหรือไม่กรอกก็ได้
แต่คราวนี้ ถ้าไม่กรอก ก็ไมสามารถ insert ได้
งง มากๆครับ
อย่างนี้เกิดจากอะไรเหรอครับ
ผมใช้ detailsView  ในการสร้าง ฟอร์ม ครับ
 
 

<asp:DetailsView ID="dtinsert" DataSourceID="InsertNewcustomer" DataKeyNames="ID"

AutoGenerateInsertButton="True" AutoGenerateRows="False" runat="server"

DefaultMode ="Insert" Width="100%" >

<



darkman69 วันที่ส่ง: 17 มี.ค. 52 15:24 GMT+7
วันที่ปรับล่าสุด: 17 มี.ค. 52 15:29 GMT+7
REPLY #1 (102561)
 

<asp:DetailsView ID="dtinsert" DataSourceID="InsertNewcustomer" DataKeyNames="ID"

AutoGenerateInsertButton="True" AutoGenerateRows="False" runat="server"

DefaultMode ="Insert" Width="100%" >

<Fields>

<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName" >

<InsertItemTemplate >

<asp:TextBox ID="txtFirstname" runat="server" Text='<%# Bind("Firstname") %>' MaxLength="50" Width="680px"></asp:TextBox>

<asp:RequiredFieldValidator ID="valRequiresex" runat="server" ControlToValidate="txtFirstname"

Display="Dynamic" ErrorMessage="RequiredFieldValidator" SetFocusOnError="True"

ToolTip="The Title field is required.">The Firstname field is required.</asp:RequiredFieldValidator>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="LastName" SortExpression="Description">

<InsertItemTemplate >

<asp:TextBox ID="txtlastname" runat="server" Text='<%# Bind("Lastname") %>' MaxLength="50" Width="680px"></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Nickname" SortExpression="Description">

<InsertItemTemplate >

<asp:TextBox ID="txtNickname" runat="server" Text='<%# Bind("Nickname") %>' MaxLength="50" Width="680px"></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="เพศ" SortExpression="CUSSEX">

<InsertItemTemplate >

<asp:DropDownList ID="ddlSEX" runat="server" AutoPostBack="False" DataValueField="CUSSEX" DataTextField="CUSSEX" SelectedValue='<%# Bind("CUSSEX") %>'>

<asp:ListItem Value ="true" Text ="ชาย" Selected ="True" ></asp:ListItem>

<asp:ListItem Value ="false" Text ="หญิง"></asp:ListItem>

</asp:DropDownList>

<%--<asp:RadioButton ID="Sex1" runat="server" Checked='<%# IIf(Eval("CUSSEX")=1,false,true) %>' Text="ชาย" GroupName="Sex" />

<asp:RadioButton ID="Sex2" runat="server" Checked='<%# IIf(Eval("CUSSEX")=0,false,true)%>' Text="หญิง" GroupName="Sex" />--%>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="BirthDAy" SortExpression="Description">

<InsertItemTemplate >

<asp:TextBox ID="txtBirthDAy" runat="server" Text='<%# Bind("BD", "{0:d}") %>' MaxLength="200" Width="680px" ></asp:TextBox><br />

<asp:CompareValidator runat="server" ID="valtxtBirthDAy" ControlToValidate="txtBirthDAy"

SetFocusOnError="true" Display="Dynamic" Operator="DataTypeCheck" Type="Date"

Text="กรอก วันที่ไม่ถูกต้องนะครับ ดด/วว/ปปปป ครับ"

ToolTip="กรอก วันที่ไม่ถูกต้องนะครับ ดด/วว/ปปปป ครับ" />

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="address" SortExpression="Description">

<InsertItemTemplate >

<asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("Address") %>' MaxLength="4000" Rows="5" TextMode="MultiLine" Width="680px" ></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Category" SortExpression="CategoryID">

<InsertItemTemplate >

<asp:DropDownList ID="ddlCategories" runat="server" DataSourceID="ObjectDataSource1"

DataTextField="Title" DataValueField="ID" SelectedValue='<%# Bind("CustypeID") %>' />

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="phone" SortExpression="Description">

<InsertItemTemplate >

<asp:TextBox ID="txtPhone" runat="server" Text='<%# Bind("Phone") %>' MaxLength="4000" Width="680px" ></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="mobile" SortExpression="Description">

<InsertItemTemplate >

<asp:TextBox ID="txtMoblie" runat="server" Text='<%# Bind("Moblie") %>' MaxLength="4000" Width="680px" ></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="email" SortExpression="Description">

<InsertItemTemplate >

<asp:TextBox ID="txtEMail" runat="server" Text='<%# Bind("EMail") %>' MaxLength="4000" Width="680px" ></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="ส่วนสูง(ซ.ม.)" SortExpression="ส่วนสูง">

<InsertItemTemplate >

<asp:TextBox ID="txtHeight" runat="server" Text='<%# Bind("Height") %>' MaxLength="10" Width="680px" ></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="น้ำหนัก(ก.ก.)" SortExpression="น้ำหนัก">

<InsertItemTemplate >

<asp:TextBox ID="txtWeight" runat="server" Text='<%# Bind("Weight") %>' MaxLength="10" Width="680px" ></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="ประวัติการแพ้ยา" SortExpression="Description">

<InsertItemTemplate >

<asp:CheckBox ID="chkcondition" runat="server" Checked='<%# Bind("Condition") %>'/>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="ประวัติกระดูก" SortExpression="Description">

<InsertItemTemplate >

<asp:CheckBox ID="chkBone" runat="server" Checked='<%# Bind("Bone") %>' />

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="จุดประสงค์ ในการเข้ารับบริการ" SortExpression="จุดประสงค์ ในการเข้ารับบริการ">

<InsertItemTemplate >

<asp:TextBox ID="txtGoal" runat="server" Text='<%# Bind("Goal") %>' MaxLength="4000" Rows="5" TextMode="MultiLine" Width="680px" ></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="NOTE" SortExpression="NOTE">

<InsertItemTemplate >

<asp:TextBox ID="txtCusnote" runat="server" Text='<%# Bind("Cusnote") %>' MaxLength="4000" Rows="5" TextMode="MultiLine" Width="680px" ></asp:TextBox>

</InsertItemTemplate>

</asp:TemplateField>

</Fields>

</asp:<



darkman69 วันที่ส่ง: 17 มี.ค. 52 15:29 GMT+7
วันที่ปรับล่าสุด: 17 มี.ค. 52 15:29 GMT+7
กระทู้นี้มีอายุเกิน 365 วันแล้ว ท่านจะไม่สามารถตอบกระทู้นี้ได้อีก
ถ้าต้องการสนทนาต่อ กรุณาตั้งเป็นกระทู้ใหม่ได้ในหน้าโฮม
และอาจจะอ้างถึงกระทู้นี้ โดยก๊อปปี้ข้อความในกล่องสีขาวด้านล่างไปแปะในกระทู้ใหม่

copy เพื่ออ้างอิงถึงข้อความนี้:

8 ก.ย. 19:46
Online: 158