I am trying to add a record to a database from a asp textbox with a asp button click. If I run the page with "start debugging" it runs fine, if I "view in browser" I need to click the button twice to get a postback. Should I believe the debugger or view
in browser? I have tried the site on two separate computers with the same result. Code is below.
Imports System.Data.Linq
Partial Class GridView
Inherits System.Web.UI.Page
Protected Sub AddFName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddFName.Click
Dim db As New EmployeesDataContext
Dim p As New FName With {.Name = Me.FNameText.Text}
It seems that you've created two instances of Datacontent. In fact you don't need to do so. Just change your codes like this following:
Protected Sub AddFName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddFName.Click
Dim db As New EmployeesDataContext
Dim p As New FName With {.Name = Me.FNameText.Text}
db.FNames.InsertOnSubmit(p)
db.SubmitChanges()
Dim result = From item in db.xxx …………
GridView1.DataSource = result
GridView1.DataBind()
End Sub
JDGcoder
0 Points
1 Post
Do I believe "start debugging" or "view in browser"?
Jan 06, 2013 04:59 AM|LINK
I am trying to add a record to a database from a asp textbox with a asp button click. If I run the page with "start debugging" it runs fine, if I "view in browser" I need to click the button twice to get a postback. Should I believe the debugger or view in browser? I have tried the site on two separate computers with the same result. Code is below.
Imports System.Data.Linq
Partial Class GridView
Inherits System.Web.UI.Page
Protected Sub AddFName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddFName.Click
Dim db As New EmployeesDataContext
Dim p As New FName With {.Name = Me.FNameText.Text}
db.FNames.InsertOnSubmit(p)
db.SubmitChanges()
UpdateData()
End Sub
Protected Sub UpdateData()
Dim db As New EmployeesDataContext
Dim fn = From p In db.FNames _
Order By p.FName Ascending _
Select New With {p.FName}
GridView1.DataSource = fn
GridView1.DataBind()
End Sub
End Class
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="GridView.aspx.vb" Inherits="GridView" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:TextBox ID="FNameText" runat="server" AutoPostBack="True">
</asp:TextBox>
<asp:Button ID="AddFName" runat="server" Text="Add" />
<p>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</p>
</asp:Content>
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Do I believe "start debugging" or "view in browser"?
Jan 07, 2013 12:49 AM|LINK
Hi,
It seems that you've created two instances of Datacontent. In fact you don't need to do so. Just change your codes like this following:
Protected Sub AddFName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddFName.Click Dim db As New EmployeesDataContext Dim p As New FName With {.Name = Me.FNameText.Text} db.FNames.InsertOnSubmit(p) db.SubmitChanges() Dim result = From item in db.xxx ………… GridView1.DataSource = result GridView1.DataBind() End Sub