พี่ๆๆๆที่รักค่ะช่วยดูโค้ดให้ทีค่ะ .. insert ค่าใน DropDownlist ไม่ได้ค่ะ
พี่ๆๆค่ะช่วยดูให้ทีค่ะ insertค่าใน DropDownlist ไม่ได้ค่ะ มันได้เฉพาะค่าแรกไม่ว่าเราจะเลือกค่าไหนไปมันจะinsertเป็นค่าแรกตลอดเลยค่ะแก้ไม่ได้พี่ๆๆๆที่รู้ช่วยบอกที่นะค่ะขอบคุณล่วงหน้าค่ะ
Code (VB.NET)
Imports System.Data
Imports System.Data.OleDb
Partial Class frm_insert
Inherits System.Web.UI.Page
Protected Sub crl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles crl.Click
txtbook_id.Text = ""
txtbook_name.Text = ""
txtbook_by.Text = ""
txtbook_number.Text = ""
txtbook_detail.Text = ""
txtbook_price.Text = ""
lblstatus.Text = ""
End Sub
Protected Sub save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles save.Click
Dim strConn As String
Dim strSQL As String
Dim objCmd As New OleDbCommand
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/mydatabase.mdb") & ";"
Dim objCon As New OleDbConnection(strConn)
objCon.Open()
strSQL = "INSERT INTO tb_book(book_id,book_name,book_by,book_number,book_detail,book_price,type_id)"
strSQL &= " VALUES ('" & txtbook_id.Text & "',"
strSQL &= "'" & txtbook_name.Text & "',"
strSQL &= "'" & txtbook_by.Text & "',"
strSQL &= "'" & txtbook_number.Text & "',"
strSQL &= "'" & txtbook_detail.Text & "',"
strSQL &= "'" & txtbook_price.Text & "',"
strSQL &= "'" & Me.listtype_id.SelectedItem.Value & "')"
With objCmd
.Connection = objCon
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Try
objCmd.ExecuteNonQuery()
Me.lblstatus.Text = "บันทึกข้อมูลเรียบร้อยแล้ว"
Me.lblstatus.Visible = True
Response.Redirect("frm_insert.aspx")
Catch ex As Exception
Me.lblstatus.Visible = True
Me.lblstatus.Text = " ไม่สามารถบันทึกข้อมูลได้(" & ex.Message & ")"
End Try
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objCon As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim strCon As String
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/mydatabase.mdb") & ";"
objCon = New OleDbConnection(strCon)
objCon.Open()
Dim strSQL As String
strSQL = "SELECT *FROM tb_type"
dtAdapter = New OleDbDataAdapter(strSQL, objCon)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objCon.Close()
objCon = Nothing
With Me.listtype_id
.DataSource = dt
.DataTextField = "type_name"
.DataValueField = "type_id"
.DataBind()
End With
listtype_id.SelectedIndex = listtype_id.Items.IndexOf(listtype_id.Items.FindByValue("type_id"))
End Sub
Tag : - - - -
Date :
2010-02-14 03:33:03
By :
neenarat
View :
1170
Reply :
2
มันเกิดจากตอนที่คุณกด Submit อีกครั้ง มันไปทำงานในส่วนของ Page_Load() อีกครั้งน่ะครับ เลยทำการ Bind ในส่วนของ Dropdownlist อีกรอบ
Code (VB.NET)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim objCon As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
IF NOT Page.IsPostBack() Then
Dim strCon As String
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/mydatabase.mdb") & ";"
objCon = New OleDbConnection(strCon)
objCon.Open()
Dim strSQL As String
strSQL = "SELECT *FROM tb_type"
dtAdapter = New OleDbDataAdapter(strSQL, objCon)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objCon.Close()
objCon = Nothing
With Me.listtype_id
.DataSource = dt
.DataTextField = "type_name"
.DataValueField = "type_id"
.DataBind()
End With
listtype_id.SelectedIndex = listtype_id.Items.IndexOf(listtype_id.Items.FindByValue("type_id"))
End IF
End Sub
Date :
2010-02-14 08:52:14
By :
webmaster
ขอบคุณม๊ากๆๆๆๆเรยค่ะทำได้แล้วค่ะ
Date :
2010-02-14 22:02:12
By :
neenarat
Load balance : Server 00