benshaws
Hobbyist Programmer
I have a webform that is pre-populated with data obtained from a database (A calendar event). The user can amend/change the data on the form and click a OK button that should then update the database. My code runs fine (no error messages) except the data being posted back is the original data from the page load event. I have included some message boxes to display the variables contents, so I could determine what is happening for testing purposes.
For the record I am using Visual Express Web Developer 2012. .net framework 4.0 and the database is MS Access.
I would be grateful if someone would point me in the right direction with solving this.
Thanks.
Mod edit fixed code tags. Please use the code tags provided by the editor.
For the record I am using Visual Express Web Developer 2012. .net framework 4.0 and the database is MS Access.
VB.NET:
Imports Calendar.GlobalVariables
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ds As New DataSet
Dim strQuery As String = "SELECT * FROM Calendar WHERE itemTitle = '" & strSelectedTitle & "' AND itemDate = '" & strSelectedDate & "'"
Dim da As New OleDb.OleDbDataAdapter(strQuery, strConnection)
strConnection.Close()
strConnection.Open()
'Create a dataset.
da.Fill(ds, "Calendar")
da.Dispose()
strConnection.Close()
'Display the results in the edit event form.
_Date.SelectedDate = strSelectedDate
ClrTitle.Text = strSelectedTitle
Details.Text = (ds.Tables.Item("Calendar").Rows.Item(0).Item("itemDetails")).ToString
ds.Dispose()
End Sub
Protected Sub Do_Update(sender As Object, e As EventArgs) Handles BtnUpdate.Click
'Only update the database if the user entered valid inputs
If Not Page.IsValid Then Exit Sub
'strSelectedDate is a Shared Public As Date variable.
'strSelectedTitle is a Shared Public As String variable.
Dim strNewDate As Date = (_Date.SelectedDate).ToString
Dim strNewTitle As String = ClrTitle.Text
Dim strNewDetails As String = Details.Text
Dim strUpdate As String = "UPDATE Calendar SET itemDate ='" & strNewDate & _
"', itemTitle ='" & strNewTitle & _
"', itemDetails ='" & strNewDetails & _
"' WHERE itemDate ='" & strSelectedDate & _
" AND itemTitle = '" & strSelectedTitle & "'"
Dim myCommand As New OleDb.OleDbCommand
MsgBox(strNewDate) 'THIS STILL HOLDS THE VARIABLE FROM THE PAGE LOAD EVENT EVEN IF CHANGED BY THE USER.
MsgBox(strNewTitle) 'THIS STILL HOLDS THE VARIABLE FROM THE PAGE LOAD EVENT EVEN IF CHANGED BY THE USER.
MsgBox(strNewDetails) 'THIS STILL HOLDS THE VARIABLE FROM THE PAGE LOAD EVENT EVEN IF CHANGED BY THE USER.
myCommand.CommandText = strUpdate
myCommand.Connection = strConnection
strConnection.Close()
strConnection.Open()
myCommand.ExecuteNonQuery()
strConnection.Close()
Response.Redirect("~/default.aspx") 'Redirect the user to the home page.
End Sub
End Class
HTML:
<div>
<table class="calendar" style="margin-left: auto; margin-right: auto;">
<tr class="calendar">
<td class="calendar" style="vertical-align: top"></td>
<td class="auto-style3" style="vertical-align: top; margin-left: auto; margin-right: auto; padding: 0px">
<asp:Calendar ID="_Date" runat="server" AutoPostBack="True" Height="167px" Width="442px" Style="margin-left: auto; margin-right: auto; padding: 0px" BackColor="White" BorderColor="Black" BorderStyle="Solid" CellSpacing="1" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" NextPrevFormat="FullMonth">
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" Height="8pt" />
<DayStyle BackColor="#CCCCCC" />
<NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#999999" />
<SelectedDayStyle BackColor="#333399" ForeColor="White" />
<TitleStyle BackColor="#333399" BorderStyle="Solid" Font-Bold="True" Font-Size="12pt" ForeColor="White" Height="12pt" />
<TodayDayStyle BackColor="#999999" ForeColor="White" />
</asp:Calendar>
</td>
</tr>
<tr class="calendar">
<td class="calendar" style="vertical-align: middle">Description</td>
<td class="auto-style3" style="vertical-align: top">
<asp:TextBox Width="100%" ID="ClrTitle" runat="server" /><br />
</td>
</tr>
<tr class="calendar">
<td class="calendar" style="vertical-align: top"></td>
<td class="auto-style3" style="vertical-align: top;color:red;">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="ClrTitle" runat="server">This is a required field</asp:RequiredFieldValidator>
</td>
</tr>
<tr class="calendar">
<td class="calendar" style="vertical-align: top">Notes</td>
<td class="auto-style3" style="vertical-align: top">
<asp:TextBox ID="Details" runat="server" Height="146px" Width="100%" style="" TextMode="MultiLine"/>
</td>
</tr>
<tr class="calendar">
<td class="auto-style4"></td>
<td class="auto-style5" style="vertical-align: top">
<asp:Button ID="BtnUpdate" Text="OK" OnClick="Do_Update" runat="server" Width="100px" />
<asp:Button ID="BtnDelete" Text="Delete" OnClick="Do_Delete" runat="server" Width="100px" />
</td>
</tr>
</table>
</div>
I would be grateful if someone would point me in the right direction with solving this.
Thanks.
Mod edit fixed code tags. Please use the code tags provided by the editor.
Last edited by a moderator: