|
|
|
ไม่สามารถ Update ข้อมูลใหม่ลงในฐานข้อมูลได้เพราะ textbox ที่สร้างขึ้นมาใน Code ของ .aspx.vb มันเก็บแ |
|
|
|
|
|
|
|
ไม่สามารถ Update ข้อมูลใหม่ลงในฐานข้อมูลได้เพราะ textbox ที่สร้างขึ้นมาใน Code ของ .aspx.vb มันเก็บแต่ค่าเก่าไม่ยอมเก็บค่าใหม่ให้
.aspx
<asp:GridView ID="dgvBranch" runat="server"
AutoGenerateColumns="False" DataKeyNames="Branch_ID"
Width="976px" AllowPaging="True"
AllowSorting="True" HorizontalAlign="Center"
OnPageIndexChanging="PageBranch"
OnRowUpdating="modUpdateCommandBranch">
<Columns>
<asp:BoundField DataField="Branch_ID" HeaderText="รหัสสาขา" ReadOnly="True">
<ItemStyle Width="79px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Branch Name">
<ItemTemplate>
<asp:Label ID="lblBranchEN" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Branch_EN") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="350px" />
<EditItemTemplate>
<asp:TextBox ID="txtEBranchEN" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Branch_EN") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ชื่อสาขา">
<ItemTemplate>
<asp:Label ID="lblBranchEN" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Branch_TH") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="334px" /> <EditItemTemplate>
<asp:TextBox ID="txtEBranchTH" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Branch_TH") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="คำย่อสาขา">
<ItemTemplate>
<asp:Label ID="lblBranchCode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Branch_Code") %>'></asp:Label>
</ItemTemplate> <EditItemTemplate>
<asp:TextBox ID="txtEBranchCode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Branch_Code") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True"
CancelText="Cancel" DeleteText="Delete" UpdateText="Update"/>
<asp:CommandField ShowDeleteButton="True" SortExpression="Branch_ID" />
</Columns>
<PagerStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle BackColor="#CCFFCC" />
</asp:GridView>
.aspx.vb
Sub modUpdateCommandBranch(ByVal s As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
Dim txtGBranchEN As TextBox = CType(dgvBranch.Rows(e.RowIndex).FindControl("txtEBranchEN"), TextBox)
Dim txtGBranchTh As TextBox = CType(dgvBranch.Rows(e.RowIndex).FindControl("txtEBranchTH"), TextBox)
Dim txtGBranchCode As TextBox = CType(dgvBranch.Rows(e.RowIndex).FindControl("txtEBranchCode"), TextBox)
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
strSQL = ""
strSQL = "UPDATE Branch SET "
strSQL &= "Branch_EN = '" & txtGBranchEN.Text & "',"
strSQL &= "Branch_TH = '" & txtGBranchTh.Text & "',"
strSQL &= "Branch_Code = '" & txtGBranchCode.Text & "' "
strSQL &= "WHERE Branch_ID = '" & dgvBranch.DataKeys.Item(e.RowIndex).Value & "'"
txtBranchEN.Text = strSQL
cmd = New SqlCommand(strSQL, conn)
cmd.ExecuteNonQuery()
dgvBranch.EditIndex = -1
dgvBranch.ShowFooter = True
dgvBranch.DataBind()
End Sub
ไม่ทราบว่า Code ตรงที่ Dim Textbox ไว้ใน .aspx.vb ผิดหรือเปล่าค่ะมันถึงไม่ยอมเก็บค่าใหม่ให้
ขอรบกวนช่วยดูให้หน่อยนะค่ะ
Tag : - - - -
|
|
|
|
|
|
Date :
2010-03-10 12:10:45 |
By :
wiphawan |
View :
1556 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองใช้
IF NOT Page.IsPostBack() Then
End IF
ตัวอย่าง
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim dtReader As OleDbDataReader
Dim strConnString,strSQL As String
Sub Page_Load(sender As Object, e As EventArgs)
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
objConn.ConnectionString = strConnString
objConn.Open()
IF Not Page.IsPostBack() Then
ViewData()
End IF
End Sub
Sub ViewData()
'*** DataTable ***'
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
strSQL = "SELECT * FROM customer WHERE CustomerID = '"& Request.QueryString("CustomerID") &"' "
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
Me.txtCustomerID.Text = dt.Rows(0)("CustomerID")
Me.txtName.Text = dt.Rows(0)("Name")
Me.txtEmail.Text = dt.Rows(0)("Email")
Me.txtCountryCode.Text = dt.Rows(0)("CountryCode")
Me.txtBudget.Text = dt.Rows(0)("Budget")
Me.txtUsed.Text = dt.Rows(0)("Used")
End IF
End Sub
Sub btnSave_Click(sender As Object, e As EventArgs)
strSQL = "UPDATE customer SET " & _
" CustomerID = '"& Me.txtCustomerID.Text &"' " & _
" ,Name = '"& Me.txtName.Text &"' " & _
" ,Email = '"& Me.txtEmail.Text &"' " & _
" ,CountryCode = '"& Me.txtCountryCode.Text &"' " & _
" ,Budget = '"& Me.txtBudget.Text &"' " & _
" ,Used = '"& Me.txtUsed.Text &"' " & _
" WHERE CustomerID = '" & Request.QueryString("CustomerID") & "' "
objCmd = New OleDbCommand
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Me.pnlAdd.Visible = False
Try
objCmd.ExecuteNonQuery()
Me.lblStatus.Text = "Record Updated"
Me.lblStatus.Visible = True
Catch ex As Exception
Me.lblStatus.Text = "Record can not update"
End Try
End Sub
Sub Page_UnLoad()
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Microsoft Access</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel id="pnlAdd" runat="server">
<table width="353" border="1">
<tbody>
<tr>
<td width="102">
<asp:Label id="lblCustomerID" runat="server" text="CustomerID"></asp:Label></td>
<td width="235">
<asp:TextBox id="txtCustomerID" runat="server" Width="79px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblName" runat="server" text="Name"></asp:Label></td>
<td>
<asp:TextBox id="txtName" runat="server" Width="177px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblEmail" runat="server" text="Email"></asp:Label></td>
<td>
<asp:TextBox id="txtEmail" runat="server" Width="155px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblCountryCode" runat="server" text="CountryCode"></asp:Label></td>
<td>
<asp:TextBox id="txtCountryCode" runat="server" Width="38px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblBudget" runat="server" text="Budget"></asp:Label></td>
<td>
<asp:TextBox id="txtBudget" runat="server" Width="76px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblUsed" runat="server" text="Used"></asp:Label></td>
<td>
<asp:TextBox id="txtUsed" runat="server" Width="76px"></asp:TextBox>
</td>
</tr>
</tbody>
</table>
<br />
<asp:Button id="btnSave" onclick="btnSave_Click" runat="server" Text="Save"></asp:Button>
<br />
</asp:Panel>
<asp:Label id="lblStatus" runat="server" visible="True"></asp:Label>
</form>
</body>
</html>
มาควบคุม Event ในการ Submit
ASP.NET Microsoft Access Edit/Update Record
|
|
|
|
|
Date :
2010-03-10 18:12:48 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|