ASP.net Syntax error in INSERT INTO statement - cmd.ExecuteNonQuery() - asp.net

Currently doing a project and I am having a serious problem with some code. I keep getting the cmd.ExecuteNonQuery() and i have searched all over the internet to find the solution. I have inserted [] in certain catagories and still produces the same error.
All I am doing is creating a page where people can input data for certain catagories, in which once added, it will be updated to the access database. I had this working on an earlier version but ever since i added more characters, it has come with this error.
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="addcar.aspx.vb" Inherits="WebApplication1.addcar" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
<p>
<br />
CarName<asp:TextBox ID="tb_CarName" runat="server"></asp:TextBox>
</p>
<p>
<br />
Year<asp:TextBox ID="tb_Year" runat="server"></asp:TextBox>
</p>
<p>
<br />
EngineSize<asp:TextBox ID="tb_EngineSize" runat="server"></asp:TextBox>
</p>
<p>
<br />
BHP<asp:TextBox ID="tb_BHP" runat="server"></asp:TextBox>
</p>
<p>
<br />
Origin<asp:TextBox ID="tb_Origin" runat="server"></asp:TextBox>
</p>
<p>
<br />
Description<asp:TextBox ID="tb_Description" runat="server"></asp:TextBox>
</p>
<p>
<br />
Pictures<asp:TextBox ID="tb_Pictures" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button" runat="server" Text="Add Car Details" />
</p>
</asp:Content>
Followed by the button code which is:
Imports System.Data.OleDb
Public Class addcar
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("CarsConnectionString").ConnectionString)
Dim SqlString As String = "Insert into Table1(CarName,Year,EngineSize,BHP,Origin,Description,Pictures) Values (#f1,#f2,#f3,#f4,#f5,#f6,#f7)"
Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("#f1", tb_CarName.Text)
cmd.Parameters.AddWithValue("#f2", tb_Year.Text)
cmd.Parameters.AddWithValue("#f3", tb_EngineSize.Text)
cmd.Parameters.AddWithValue("#f4", tb_BHP.Text)
cmd.Parameters.AddWithValue("#f5", tb_Origin.Text)
cmd.Parameters.AddWithValue("#f6", tb_Description.Text)
cmd.Parameters.AddWithValue("#f7", tb_Pictures.Text)
oleDbConn.Open()
cmd.ExecuteNonQuery()
Response.Redirect("confirmation.aspx")
End Sub
End Class
Help? What am I doing wrong?
Error is as follows:
Syntax error in INSERT INTO statement.

Year is a reserved keyword in access / Jet.
You need to encapsulate it with square brackets.
Dim SqlString As String = "Insert into Table1(CarName, [Year],EngineSize,BHP,Origin,Description,Pictures) Values (#f1,#f2,#f3,#f4,#f5,#f6,#f7)"
You can check every reserved keyword in the jet engine here:
http://support.microsoft.com/kb/248738

Related

Dynamic page creation from db at run time in asp.net

I'm making E-Commerce website. Under that I want to show particular product details. From query string I can do that but that is not seo friendly so I need to make product name in url(from db) instead of using querystring. I tried following code but it is not working for me.
Error - The resource could not be Found
Global.asax
<%# Application Language="VB" %>
<%# Import Namespace="System.Web.Optimization" %>
<%# Import Namespace="System.Web.Routing" %>
<script runat="server">
Sub Application_Start(sender As Object, e As EventArgs)
RouteConfig.RegisterRoutes(RouteTable.Routes)
BundleConfig.RegisterBundles(BundleTable.Bundles)
End Sub
Private Shared Sub RegisterRoutes(routes As RouteCollection)
routes.MapPageRoute("product-detail", "{product_name}.aspx", "product-detail.aspx")
End Sub
</script>
product-detail.aspx (Dynamic Page)
Private Sub product_detail_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim pageName As String = Me.Page.RouteData.Values("product_name").ToString()
End If
End Sub
shop.aspx (used listview control to display list of products)
<asp:ListView ID="products" runat="server" DataKeyNames="ID">
<ItemTemplate>
<asp:HyperLink ID="productID" runat="server" NavigateUrl='<%# Eval("product_name", "~/{0}") %>' CssClass="product-link">
<!--blocks-starts--><div class="blocks blocks-shop">
<asp:Image ID="readyStock" runat="server" ImageUrl="images/common/ready_stock_tag.png" Visible="false" CssClass="tag" />
<asp:Label ID="checkReadyStock" runat="server" Visible="false"></asp:Label>
<div class="block-img">
<img src='<%# Eval("image") %>' runat="server" id="proImg" />
</div>
<div class="block-content">
<span class="sku" style="font-size:0.6em !important">Item No. <asp:Label ID="skuID" runat="server" Text='<%# Eval("sku") %>'></asp:Label></span>
<h3>
<asp:Label ID="prodName" runat="server" Text='<%# Eval("product_name") %>'></asp:Label></h3>
<p><strong>
<asp:Label ID="priceRange" runat="server" Text='<%# Eval("price_range") %>'></asp:Label></strong></p>
</div>
</div><!--blocks-ends-->
</asp:HyperLink>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style="">
<div runat="server" id="itemPlaceholder" />
</div>
</LayoutTemplate>
</asp:ListView>
shop.aspx.vb
Private Sub shop_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Try
Dim str As String = "select * from products where status = 'active'"
Dim cmd As New MySqlCommand(str, con)
con.Open()
Dim da As New MySqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
products.DataSource = dt
products.DataBind()
con.Close()
Catch ex As Exception
Response.Write(ex)
End Try
End If
End Sub

ListView Datapager not working When Session used instead of VeiwState

I want to display list of clinics details in listview. From MasterPage Users selects the city & If he goes to clinincs page then list of hospitals display from selected city. Now If I use ViewState in my clinics page then It does not gets clinics data from selected city So what I have done I store City session & on clinics page I have used Session instead of ViewState Which is now working right but my datagaer stopped working. If I have to see another page from datapager then it doesn't switch over. Here is my code
MasterPage
Protected Sub locationSelector_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles locationSelector.TextChanged
Session("masterLocation") = locationSelector.text
Session("Data") = Nothing
End Sub
Clinics Page
Private Sub hospitals_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
Try
If Not Session("Data") Is Nothing Then
hospitals.DataSource = Session("Data")
hospitals.DataBind()
Else
Dim citySelector As Label = Page.Master.FindControl("locationPopupActivator")
query = "select hospitalid, name, address, thumbnail, serviceID, mondayFrom, mondayTo, consultancyFees, city from hospitals Where city Like '" + citySelector.Text + "%' and status = 'active'"
Dim cmd As New MySqlCommand(query, con)
cmd.CommandTimeout = 120
Dim da As New MySqlDataAdapter(cmd)
Dim table As New DataTable
da.Fill(table)
Session("Data") = table
hospitals.DataSource = table
hospitals.DataBind()
End If
'mainCount.Text = table.Rows(0)("countRows").ToString
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
Protected Sub DataPager1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Try
Dim table As DataTable = Session("Data")
hospitals.DataSource = table
hospitals.DataBind()
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
Listview on ASPX
<asp:ListView ID="hospitals" runat="server" DataKeyNames="hospitalID" DataKey="hospitalID">
<ItemTemplate>
My Content
</ItemTemplate>
<EmptyDataTemplate>
<div class="not-found">
<p>
Sorry! Selected Query Not Found</p>
<center>
<img src="images/not-found.jpg" /></center>
</div>
</EmptyDataTemplate>
<LayoutTemplate>
<ul id="itemPlaceholderContainer" runat="server" style="">
<li runat="server" id="itemPlaceholder" />
</ul>
<div class="datapager" style="padding-bottom: 10px;">
<asp:DataPager ID="DataPager1" runat="server" PageSize="10" PagedControlID="hospitals" ViewStateMode="Enabled">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
</asp:ListView>
This is working sample of your page:
hospital.master
<%# Master Language="VB" CodeFile="hospital.master.vb" Inherits="hospital_master" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server" />
<div>
<asp:Label ID="lblLockSel" runat="server" AssociatedControlID="locationSelector">Locator Selector</asp:Label>
<asp:UpdatePanel ID="upLocSel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="locationSelector" runat="server" AutoPostBack="true"></asp:TextBox>
<asp:Label ID="locationPopupActivator" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
hospital.master.vb
Partial Class hospital_master
Inherits System.Web.UI.MasterPage
Protected Sub locationSelector_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles locationSelector.TextChanged
Session("masterLocation") = locationSelector.Text
Session("Data") = Nothing
locationPopupActivator.Text = locationSelector.Text
End Sub
End Class
clinics.aspx
<%# Page Title="" Language="VB" MasterPageFile="~/hospital.master" AutoEventWireup="false" CodeFile="clinics.aspx.vb" Inherits="Clinics" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:UpdatePanel ID="upHospitals" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:ListView ID="hospitals" runat="server" DataKeyNames="hospitalID" DataKey="hospitalID">
<ItemTemplate>
<%--My Content--%>
<li>
<asp:HyperLink ID="lnkHospital" runat="server" NavigateUrl='<%#Eval("hospitalID", "~/hospital.aspx?id={0}") %>' Text='<%#Eval("Name") %>'></asp:HyperLink>
</li>
</ItemTemplate>
<EmptyDataTemplate>
<div class="not-found">
<p>
Sorry! Selected Query Not Found</p>
<center><%--<center>Is Deprecated Use div with CSS</center>--%>
<img src="images/not-found.jpg" /></center>
</div>
</EmptyDataTemplate>
<LayoutTemplate>
<ul>
<li runat="server" id="itemPlaceholder" />
</ul>
</LayoutTemplate>
</asp:ListView>
<%--Inside Update Panel dataPager should be outside paged control--%>
<div class="datapager" style="padding-bottom: 10px;">
<asp:DataPager ID="DataPager1" runat="server" PageSize="10" PagedControlID="hospitals"><%-- ViewStateMode="Enabled"> this is default--%>
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
clinics.aspx.vb
Partial Class Clinics
Inherits System.Web.UI.Page
Private Sub Clinics_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Session.Remove("Data") 'init
End If
End Sub
Private Sub hospitals_PagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs) Handles hospitals.PagePropertiesChanging
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
hospitals.DataSource = GetData() 'Session("Data")
hospitals.DataBind()
End Sub
Private Sub hospitals_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
hospitals.DataSource = GetData()
hospitals.DataBind()
End Sub
Private Function GetData() As Data.DataTable
Try
If Session("Data") IsNot Nothing Then
Return Session("Data")
Else
'This is MS SQL server. If you use MySQL then change types accordingly
DataPager1.SetPageProperties(0, DataPager1.PageSize, False) 'reinit
Dim citySelector As Label = Page.Master.FindControl("locationPopupActivator")
Using con As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ClinicsCNN").ConnectionString)
Dim cmd As New Data.SqlClient.SqlCommand("select hospitalid, name, address, thumbnail, serviceID, mondayFrom, mondayTo, consultancyFees, city from hospitals Where city Like #city and status = 'active'", con)
cmd.CommandType = Data.CommandType.Text
cmd.Parameters.Add("#city", Data.SqlDbType.VarChar, 50).Value = citySelector.Text & "%" 'same as Session("masterLocation")
Dim table As New Data.DataTable()
Dim da As New Data.SqlClient.SqlDataAdapter(cmd)
da.Fill(table)
da.Dispose()
cmd.Dispose()
Session("Data") = table
Return table
End Using
End If
Catch ex As Exception
Response.Write(ex) 'for debug purpose
Return Nothing
End Try
End Function
End Class
Do you accept this solution?
Ok, after your comments here is my code that works for me:
Private Sub hospitals_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
Try
If Session("Data") Is Nothing Then
Dim citySelector As Label = Page.Master.FindControl("locationPopupActivator")
query = "select hospitalid, name, address, thumbnail, serviceID, mondayFrom, mondayTo, consultancyFees, city from hospitals Where city Like '" + citySelector.Text + "%' and status = 'active'"
Dim cmd As New MySqlCommand(query, con)
cmd.CommandTimeout = 120
Dim da As New MySqlDataAdapter(cmd)
Dim table As New DataTable
da.Fill(table)
Session("Data") = table
hospitals.DataSource = table
hospitals.DataBind()
End If
'mainCount.Text = table.Rows(0)("countRows").ToString
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
protected void hospitals_PagePropertiesChanging(object sender, System.Web.UI.WebControls.PagePropertiesChangingEventArgs e)
{
var datapager = ((DataPager)hospitals.FindControl("DataPager1"));
datapager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
hospitals.DataSource = Session["Data"];
hospitals.DataBind();
datapager.DataBind();
}
I don't think you need DataPager1_PreRender method at all so probably you should remove it all together. You will need to attach new event to your list view: OnPagePropertiesChanging="hospitals_PagePropertiesChanging". Sorry for code in c#, but I don't know VB very well ;)

Visual Basic subprocedure issue

I'm working on some .NET code in visual studio web developer and I'm having an issue. When I double click on a button in design view, instead of loading a subprocedure, all it does is highlight this: <asp:Button ID="btnEnter" runat="server" Text="Enter" onclick="btnEnter_Click" />
When I try to run that just to see what happens, I get this error:
Line 8: <asp:Button ID="btnEnter" runat="server"
Compiler Error Message: BC30456: 'btnEnter_Click' is not a member of 'ASP.default_aspx'.
If I erase the onclick="btnEnter_Click" and run it, it works. Either way though, when I double click the button / element, shouldn't it create a subprocedure for me that looks something like?
Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
End Sub
I've tried entering that manaully but the keywords don't turn blue or any sort of color, and when I run it, it just shows up as text on the webform with my other elements. Here is all I have so far:
<%# Page Title="Home Page" Language="VB"%>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Enter a person's name below"></asp:Label>
<p>
<asp:TextBox ID="txtStudentName" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="btnEnter" runat="server"
Text="Enter" onclick="btnEnter_Click" />
</p>
<p>
<asp:Button ID="btnDisplay" runat="server" Text="Display all and exit" />
</p>
<p>
<asp:Label ID="lbl2" runat="server" Text=" "></asp:Label>
</p>
<p>
<asp:Label ID="lbl3" runat="server" Text=" "></asp:Label>
</p>
<p>
<asp:Label ID="lbl4" runat="server" Text=" "></asp:Label>
</p>
<p>
<asp:Label ID="lbl5" runat="server" Text=" "></asp:Label>
</p>
</form>
Edit:
When I enter my code inside the default.aspx.vb file, it highlights keywords and such, but it can't reference the elements I created in the design view.
Partial Class _Default
Inherits System.Web.UI.Page
End Class
Class Lab1
Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
End Sub
Public Const length As Integer = 3
Shared counter2 As Integer = 0
Public Shared studentList As String() = New String(2) {}
Protected Sub btnEnter_Click(sender As Object, e As EventArgs)
Label1.Text = "Enter a person's name"
Dim studentName As [String] = txtStudentName.Text
If studentList.Length <= length Then
If txtStudentName.Text <> "" Then
Dim match As [Boolean] = True
Dim i As Integer = 0
While counter2 >= i
If studentList(i) IsNot Nothing Then
If studentList(i).ToUpper() = txtStudentName.Text.ToUpper() Then
match = False
Label1.Text = "This name has already been used"
End If
End If
i += 1
End While
If match = True Then
studentList(counter2) = txtStudentName.Text
counter2 += 1
End If
End If
End If
End Sub
End Class
Your Page declaration seems to say you are using inline style:
<%# Page Title="Home Page" Language="VB"%>
Which means your code is/should be within aspx file itself in a script runat="server" tag:
<%# Page Language="VB" %>
<!DOCTYPE html>
<script runat="server">
Protected Sub Button1_Click(sender As Object, e As EventArgs)
'Do something
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
However, your other code indicates you seem to want to use code-behind model, where you have .vb files (e.g. foo.aspx.vb)
The Page is declared like this for web sites (for web applications it will say CodeBehind="foo.aspx.vb"):
<%# Page Language="VB" AutoEventWireup="false" CodeFile="foo.aspx.vb" Inherits="foo" %>
You will find a foo.asp.vb file in your project
it's class name will be the name of your file (foo)
your code should be scoped to this class (I'm not sure what Class Lab1 in your post above is for....
foo.aspx.vb:
Partial Class foo
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Do something
End Sub
End Class
The "fix" is for you to:
choose which model you want to write code: inline or code-behind
declare the Page appropriately
for inline, your code will be in the aspx file itself
for code-behind, your code will live, and should be encapsulated within the Class file of the page.
The Page declaration actually says so:
CodeFile="foo.aspx.vb" is the file where the code is
Inherits="foo" is the Partial Class in the file (Partial Class foo)
Try this
Private Sub btnEnter_Click(sender As Object, e As System.EventArgs) Handles btnEnter.Click
End Sub
Here, btnEnter_Click is your event name and use the Handles keyword at the end of a procedure declaration to cause it to handle events raised by an object variable

ASP.NET VB add button that has removechild() function

I just want to know how to add a button that has a removechild() or replacechild() function on my XML. Been searching for this for a week now.
Any suggestions would be greatly appreciated. Thanks :)
This is my xml file content
<?xml version="1.0" encoding="utf-8"?>
<theNews>
<news>
<contents>News3 Contents</contents>
<title>News3 Title</title>
<pubDate>February 13, 2012</pubDate>
</news>
<news>
<contents>News2 Contents</contents>
<title>News2 Title</title>
<pubDate>February 1, 2012</pubDate>
</news>
<news>
<contents>News1 Contents</contents>
<title>News1 Title</title>
<pubDate>January 22, 2012</pubDate>
</news>
</theNews>
This is the code for my News Updater
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="NewsUpdater.aspx.vb" Inherits="NewsUpdater" %>
<%-- Add content controls here --%><asp:Content ID="Content1" runat="server"
contentplaceholderid="ContentPlaceHolder1">
<p>
</p>
<table align="center" style="width: 63%">
<tr>
<td align=center>
<div>
<h3>News Title</h3>
<asp:TextBox runat="server" ID="txtTitle" Width="308px"></asp:TextBox>
<br />
<h3>News Content</h3>
<asp:TextBox runat="server" ID="txtContents" TextMode="MultiLine" Height="119px" Width="513px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Update News" />
</div></td>
</tr>
</table>
<p>
<br />
</p>
And this is the code behind the NewsUpdater button
Partial Class NewsUpdater
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xmlFile As New System.Xml.XmlDocument()
xmlFile.Load("D:\My Documents\PresentableII\NewsContent.xml")
Dim theNewsTag As System.Xml.XmlElement = xmlFile.CreateElement("news")
Dim theTitleTag As System.Xml.XmlElement = xmlFile.CreateElement("title")
Dim theContentsTag As System.Xml.XmlElement = xmlFile.CreateElement("contents")
Dim theTitleText As System.Xml.XmlText = xmlFile.CreateTextNode(txtTitle.Text)
Dim theContentsText As System.Xml.XmlText = xmlFile.CreateTextNode(txtContents.Text)
theTitleTag.PrependChild(theTitleText)
theContentsTag.PrependChild(theContentsText)
theNewsTag.PrependChild(theTitleTag)
theNewsTag.PrependChild(theContentsTag)
xmlFile.DocumentElement.PrependChild(theNewsTag)
xmlFile.Save("D:\My Documents\PresentableII\NewsContent.xml")
Response.Redirect("NewsFrame.aspx")
End Sub
I want to add another button that has a function of remove or replace the last/latest entry to the xml.
So would this line do what you want?
xmlFile.DocumentElement.RemoveChild(xmlFile.DocumentElement.LastChild)
xmlFile.DocumentElement.AppendChild(your_replacement_element)

Making changes to a pre-populated textbox

I have the following webform with textboxes that are pre-populated on page load:
<%# Page Title="" Language="VB" MasterPageFile="~/default.master" AutoEventWireup="true" CodeFile="admin.aspx.vb" Inherits="admin" Theme="G2M" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="form1" Runat="Server">
<label>Username: </label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<label>Password: </label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<label>Product Type: </label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<label>SMTP Default Only: </label>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<label>Logo: </label>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
<asp:Button ID="submit" Text="Submit changes" runat="server" OnClick="SubmitChanges" />
</form>
</asp:Content>
And the codebehind is as follows:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim xmlDoc As New XmlDocument
xmlDoc.Load(Server.MapPath("~/XML_Config/Config.xml"))
Dim configValues As XMLParser = New XMLParser(xmlDoc) ''Instantiate the XMLParser
''Populate textboxes with XML data
TextBox1.Text = configValues.UserName
TextBox2.Text = configValues.Password
TextBox3.Text = configValues.ProductType
TextBox4.Text = configValues.SMTPDefaultOnly
TextBox5.Text = configValues.Logo
End Sub
Public Sub SubmitChanges(ByVal sender As Object, ByVal e As System.EventArgs)
Dim xmlDoc As New XmlDocument
xmlDoc.Load(Server.MapPath("~/XML_Config/Config.xml"))
Dim configValues As XMLParser = New XMLParser(xmlDoc) ''Instantiate the XMLParser
configValues.SMTPDefaultOnly = TextBox4.Text
End Sub
All I'm trying to do is make the values editable so when the form is presented to the user, they can change the values and submit them back to the file. My problem is that when the SubmitChanges function is called, even though I change the value of the textbox, it is still the same. How do I pass a new value, typed into thetextbox, to the function?
Enclose your setter in If Not ispostback in that page load. It's overwriting the boxes.

Resources