Conflicting results with identical code

N

Neo Geshel

I am having conflicting results with two pieces of identical code. One
is an insert, the other is an update to a db.

The first code, which works, is this:

Sub Add3_Click(sender As Object, e As EventArgs)
Dim imgStream as Stream = Add3Image.PostedFile.InputStream
Dim imgLen as Integer = Add3Image.PostedFile.ContentLength
Dim imgType as String = Add3Image.PostedFile.ContentType
If Not imgStream Is Nothing And imgLen > 0 And (imgType =
"image/jpeg" Or imgType = "image/pjpeg") Then
Dim imgBin() as Byte
imgBin = createThumbnail(imgStream, 200, 200)
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("INSERT INTO tblNews ([Date], [Name],
[Image], [Comment]) VALUES (@Date, @Name, @Image, @Comment)", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
request.form("Add3Date")
myCmd.Parameters.Add("@Name", OleDbType.VarWChar).Value =
RepChar(request.form("Add3Title"))
myCmd.Parameters.Add("@Image", OleDbType.LongVarBinary).Value = imgBin
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(request.form("Add3Comment"))
myCmd.ExecuteNonQuery()
myConn.Close()
Else
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("INSERT INTO tblNews ([Date], [Name],
[Comment]) VALUES (@Date, @Name, @Comment)", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
request.form("Add3Date")
myCmd.Parameters.Add("@Name", OleDbType.VarWChar).Value =
RepChar(request.form("Add3Title"))
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(request.form("Add3Comment"))
myCmd.ExecuteNonQuery()
myConn.Close()
End If
Server.Transfer(request.form("Add3Reload"))
End Sub



<h3>Add News</h3>
<table border="0" cellspacing="2" caption="Layout Table for adding data
into the database">
<tr><td class="right">Date:</td><td><input type="text" id="Add3Date"
style="display: none;" runat="server" /><img src="/images/cal.png"
height="16" width="16" alt="Choose Calendar Date" id="calendar_trigger"
/> <span id="show_date"><%= DateTime.Now.ToString("dddd, dd MMMM, yyyy")
%></span></td></tr>
<tr><td class="right">Title:</td><td><input type="text"
id="Add3Title" maxlength="50" class="required" runat="server" /></td></tr>
<tr><td class="right">Image:</td><td><input type="file"
id="Add3Image" class="required" runat="server" /></td></tr>
<tr><td class="right">id="Add3Comment"
rows="4" cols="60" class="required" runat="server"></textarea></td></tr>
<tr><input type="hidden" id="Add3Reload" runat="server"
/><td></td><td><asp:Button OnClick="Add3_Click" Text="Add News Article"
runat="server" /></td></tr>
</table>
<script type="text/javascript" src="/js/calendar.js"></script>
<script type="text/javascript" src="/js/calendar-en.js"></script>
<script type="text/javascript" src="/js/calendar-setup.js"></script>
<script language="JavaScript">
Calendar.setup({
inputField : "Add3Date",
ifFormat : "%d/%m/%Y 12:00:00 AM",
displayArea : "show_date",
daFormat : "%A, %d %B, %Y",
button : "calendar_trigger",
align : "br",
singleClick : true
});
</script>





The other code, which DOESN'T work, is this:

Sub Edit3Update(sender As Object, e As EventArgs)
Dim imgStream as Stream = Edit3Image.PostedFile.InputStream
Dim imgLen as Integer = Edit3Image.PostedFile.ContentLength
Dim imgType as String = Edit3Image.PostedFile.ContentType
If Not imgStream Is Nothing And imgLen > 0 And (imgType =
"image/jpeg" Or imgType = "image/pjpeg") Then
Dim imgBin() as Byte
imgBin = createThumbnail(imgStream, 200, 200)
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblNews SET [Date]=@Date,
[Name]=@Name, [Image]=@Image, [Comment]=@Comment WHERE [ID]=@ID", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
request.form("Edit3Date")
myCmd.Parameters.Add("@Name", OleDbType.VarWChar).Value =
RepChar(request.form("Edit3Title"))
myCmd.Parameters.Add("@Image", OleDbType.LongVarBinary).Value = imgBin
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(request.form("Edit3Comment"))
myCmd.Parameters.Add("@ID", OleDbType.Integer).Value =
request.form("Edit3ID")
myCmd.ExecuteNonQuery()
myConn.Close()
Else
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblNews SET [Date]=@Date,
[Name]=@Name, [Comment]=@Comment WHERE [ID]=@ID", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
request.form("Edit3Date")
myCmd.Parameters.Add("@Name", OleDbType.VarWChar).Value =
RepChar(request.form("Edit3Title"))
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(request.form("Edit3Comment"))
myCmd.Parameters.Add("@ID", OleDbType.Integer).Value =
request.form("Edit3ID")
myCmd.ExecuteNonQuery()
myConn.Close()
End If
' Edit3()
End Sub


<asp:Repeater ID="rptedit3post" runat="server">
<itemtemplate> <p>Preview of This Post:</p>
<blockquote>
<h5><%# Container.DataItem("Name") %></h5>
<%# NewsImage(Ctype(DataBinder.Eval(Container.DataItem,"ID"), Integer)) %>
<%# FormatDate(Container.DataItem("Date")) %>
<p><%# FormatData(Container.DataItem("Comment")) %></p>
</blockquote>
<p>Use the table below to edit the post. Click on Update to apply any
changes. Click on Delet to delete entire post.</p>
<table border="0" cellspacing="2" caption="Layout Table for adding data
into the database">
<tr><td class="right">Date:</td><td><input type="text" id="Edit3Date"
value='<%# Container.DataItem("Date") %>' style="display: none;"
runat="server" /><img src="/images/cal.png" height="16" width="16"
alt="Choose Calendar Date" id="calendar_trigger" /> <span
id="show_date"><%= DateTime.Now.ToString("dddd, dd MMMM, yyyy")
%></span></td></tr>
<tr><td class="right">Title:</td><td><input type="text"
id="Edit3Title" value='<%# Container.DataItem("Name") %>'
maxlength="50" class="required" runat="server" /></td></tr>
<tr><td class="right">Image:</td><td><input type="file"
id="Edit3Image" runat="server" /></td></tr>
<tr><td class="right">id="Edit3Comment"
rows="4" cols="60" class="required" runat="server"><%#
Container.DataItem("Comment") %></textarea></td></tr>
<tr><td><input type="hidden" id="Edit3ID" value='<%#
Container.DataItem("ID") %>' runat="server" /></td><td>
<asp:Button OnClick="Edit3Update" Text="Update News Article"
runat="server" /> &nbsp;
<asp:Button OnClick="Edit3Delete" Text="Delete News Article"
runat="server" /></td></tr>
</table>
<script type="text/javascript" src="/js/calendar.js"></script>
<script type="text/javascript" src="/js/calendar-en.js"></script>
<script type="text/javascript" src="/js/calendar-setup.js"></script>
<script language="JavaScript">
Calendar.setup({
inputField : "Edit3Date",
ifFormat : "%d/%m/%Y 12:00:00 AM",
displayArea : "show_date",
daFormat : "%A, %d %B, %Y",
button : "calendar_trigger",
align : "br",
singleClick : true
});
</script>
</itemtemplate>
</asp:Repeater>






The first one works, but the second one doesn't seem to work no matter
WHAT I try to do. As you can see, the code is nearly identical (with the
major exception of the SQL statements).

One major problem with the second one is an "Edit3Image is not defined"
error, even though it exists in the form. When I checked out the form,
however (view source in my browser), I saw that all of the ID tags had
changed dramatically - to the value plus a nonsensical string. Not only
did that ensure that "Edit3Image" couldn't be found, but that also
prevented the JavaScript (a popup calendar) from allowing the date to be
changed in "Edit3Date". How do I make both the JS and the form work
together???

BTW, the second is inside a repeater because I *need* it to be filled
with content from the database. It is the update/delete page, whereas
the first one was the add page.

Fucking frustrated

....Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
 
K

Karl Seguin [MVP]

(a) too much code, it hurts my eyes
(b) don't use bad language!
(c) I believe the problem to be simple

Edit3Image is defined in a repeater. As such you cannot simply refer to it
as Edit3Image as there are multiple versions (one for each "row" of data
bound to the repeater). How does .NET know which Edit3Image you want to
manipulate/access? If you want it to work in your javascript, you need to do
something like inputField : '<%# Edit3Date.ClientId %>' which I think will
work.

To make it work in your button click, as there are multiple rows, you'll
need to figure out which Edit3Image you want. If you want each one, you need
to loop through the items of the Repeater, if you want a specific one, you
need to jump to that index:

dim Edit3Date as HtmlInput =
ctype(myRepeater.Items[1].FindControl("Edit3Date"), HtmlInput)


Karl

--
http://www.openmymind.net/



Neo Geshel said:
I am having conflicting results with two pieces of identical code. One is
an insert, the other is an update to a db.

The first code, which works, is this:

Sub Add3_Click(sender As Object, e As EventArgs)
Dim imgStream as Stream = Add3Image.PostedFile.InputStream
Dim imgLen as Integer = Add3Image.PostedFile.ContentLength
Dim imgType as String = Add3Image.PostedFile.ContentType
If Not imgStream Is Nothing And imgLen > 0 And (imgType = "image/jpeg"
Or imgType = "image/pjpeg") Then
Dim imgBin() as Byte
imgBin = createThumbnail(imgStream, 200, 200)
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("INSERT INTO tblNews ([Date], [Name],
[Image], [Comment]) VALUES (@Date, @Name, @Image, @Comment)", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
request.form("Add3Date")
myCmd.Parameters.Add("@Name", OleDbType.VarWChar).Value =
RepChar(request.form("Add3Title"))
myCmd.Parameters.Add("@Image", OleDbType.LongVarBinary).Value = imgBin
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(request.form("Add3Comment"))
myCmd.ExecuteNonQuery()
myConn.Close()
Else
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("INSERT INTO tblNews ([Date], [Name],
[Comment]) VALUES (@Date, @Name, @Comment)", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
request.form("Add3Date")
myCmd.Parameters.Add("@Name", OleDbType.VarWChar).Value =
RepChar(request.form("Add3Title"))
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(request.form("Add3Comment"))
myCmd.ExecuteNonQuery()
myConn.Close()
End If
Server.Transfer(request.form("Add3Reload"))
End Sub



<h3>Add News</h3>
<table border="0" cellspacing="2" caption="Layout Table for adding data
into the database">
<tr><td class="right">Date:</td><td><input type="text" id="Add3Date"
style="display: none;" runat="server" /><img src="/images/cal.png"
height="16" width="16" alt="Choose Calendar Date" id="calendar_trigger" />
<span id="show_date"><%= DateTime.Now.ToString("dddd, dd MMMM, yyyy")
%></span></td></tr>
<tr><td class="right">Title:</td><td><input type="text" id="Add3Title"
maxlength="50" class="required" runat="server" /></td></tr>
<tr><td class="right">Image:</td><td><input type="file" id="Add3Image"
class="required" runat="server" /></td></tr>
<tr><td class="right">id="Add3Comment" rows="4"
cols="60" class="required" runat="server"></textarea></td></tr>
<tr><input type="hidden" id="Add3Reload" runat="server"
/><td></td><td><asp:Button OnClick="Add3_Click" Text="Add News Article"
runat="server" /></td></tr>
</table>
<script type="text/javascript" src="/js/calendar.js"></script>
<script type="text/javascript" src="/js/calendar-en.js"></script>
<script type="text/javascript" src="/js/calendar-setup.js"></script>
<script language="JavaScript">
Calendar.setup({
inputField : "Add3Date",
ifFormat : "%d/%m/%Y 12:00:00 AM",
displayArea : "show_date",
daFormat : "%A, %d %B, %Y",
button : "calendar_trigger",
align : "br",
singleClick : true
});
</script>





The other code, which DOESN'T work, is this:

Sub Edit3Update(sender As Object, e As EventArgs)
Dim imgStream as Stream = Edit3Image.PostedFile.InputStream
Dim imgLen as Integer = Edit3Image.PostedFile.ContentLength
Dim imgType as String = Edit3Image.PostedFile.ContentType
If Not imgStream Is Nothing And imgLen > 0 And (imgType = "image/jpeg"
Or imgType = "image/pjpeg") Then
Dim imgBin() as Byte
imgBin = createThumbnail(imgStream, 200, 200)
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblNews SET [Date]=@Date,
[Name]=@Name, [Image]=@Image, [Comment]=@Comment WHERE [ID]=@ID", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
request.form("Edit3Date")
myCmd.Parameters.Add("@Name", OleDbType.VarWChar).Value =
RepChar(request.form("Edit3Title"))
myCmd.Parameters.Add("@Image", OleDbType.LongVarBinary).Value = imgBin
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(request.form("Edit3Comment"))
myCmd.Parameters.Add("@ID", OleDbType.Integer).Value =
request.form("Edit3ID")
myCmd.ExecuteNonQuery()
myConn.Close()
Else
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblNews SET [Date]=@Date,
[Name]=@Name, [Comment]=@Comment WHERE [ID]=@ID", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Date", OleDbType.Date).Value =
request.form("Edit3Date")
myCmd.Parameters.Add("@Name", OleDbType.VarWChar).Value =
RepChar(request.form("Edit3Title"))
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(request.form("Edit3Comment"))
myCmd.Parameters.Add("@ID", OleDbType.Integer).Value =
request.form("Edit3ID")
myCmd.ExecuteNonQuery()
myConn.Close()
End If
' Edit3()
End Sub


<asp:Repeater ID="rptedit3post" runat="server">
<itemtemplate> <p>Preview of This Post:</p>
<blockquote>
<h5><%# Container.DataItem("Name") %></h5>
<%# NewsImage(Ctype(DataBinder.Eval(Container.DataItem,"ID"), Integer)) %>
<%# FormatDate(Container.DataItem("Date")) %>
<p><%# FormatData(Container.DataItem("Comment")) %></p>
</blockquote>
<p>Use the table below to edit the post. Click on Update to apply any
changes. Click on Delet to delete entire post.</p>
<table border="0" cellspacing="2" caption="Layout Table for adding data
into the database">
<tr><td class="right">Date:</td><td><input type="text" id="Edit3Date"
value='<%# Container.DataItem("Date") %>' style="display: none;"
runat="server" /><img src="/images/cal.png" height="16" width="16"
alt="Choose Calendar Date" id="calendar_trigger" /> <span
id="show_date"><%= DateTime.Now.ToString("dddd, dd MMMM, yyyy")
%></span></td></tr>
<tr><td class="right">Title:</td><td><input type="text" id="Edit3Title"
value='<%# Container.DataItem("Name") %>' maxlength="50" class="required"
runat="server" /></td></tr>
<tr><td class="right">Image:</td><td><input type="file" id="Edit3Image"
runat="server" /></td></tr>
<tr><td class="right">id="Edit3Comment" rows="4"
cols="60" class="required" runat="server"><%#
Container.DataItem("Comment") %></textarea></td></tr>
<tr><td><input type="hidden" id="Edit3ID" value='<%#
Container.DataItem("ID") %>' runat="server" /></td><td>
<asp:Button OnClick="Edit3Update" Text="Update News Article"
runat="server" /> &nbsp;
<asp:Button OnClick="Edit3Delete" Text="Delete News Article"
runat="server" /></td></tr>
</table>
<script type="text/javascript" src="/js/calendar.js"></script>
<script type="text/javascript" src="/js/calendar-en.js"></script>
<script type="text/javascript" src="/js/calendar-setup.js"></script>
<script language="JavaScript">
Calendar.setup({
inputField : "Edit3Date",
ifFormat : "%d/%m/%Y 12:00:00 AM",
displayArea : "show_date",
daFormat : "%A, %d %B, %Y",
button : "calendar_trigger",
align : "br",
singleClick : true
});
</script>
</itemtemplate>
</asp:Repeater>






The first one works, but the second one doesn't seem to work no matter
WHAT I try to do. As you can see, the code is nearly identical (with the
major exception of the SQL statements).

One major problem with the second one is an "Edit3Image is not defined"
error, even though it exists in the form. When I checked out the form,
however (view source in my browser), I saw that all of the ID tags had
changed dramatically - to the value plus a nonsensical string. Not only
did that ensure that "Edit3Image" couldn't be found, but that also
prevented the JavaScript (a popup calendar) from allowing the date to be
changed in "Edit3Date". How do I make both the JS and the form work
together???

BTW, the second is inside a repeater because I *need* it to be filled with
content from the database. It is the update/delete page, whereas the first
one was the add page.

Fucking frustrated

...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top