Reply
ASP.NET: SQL INSERT (WTF?) and more...
Old 06-14-2007, 09:23 AM ASP.NET: SQL INSERT (WTF?) and more...
mb2000inc's Avatar
Skilled Talker

Posts: 51
Name: Mark
Location: Ohio
Normally this is an easy task... I'm using VS 2005 (way cool, btw.)
The first thing I have an issue with is this:
Quote:
Error 1 'Public Event Click(sender As Object, e As System.Web.UI.ImageClickEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
In this version of VS, most coding is done in the html/source view and I'm trying to call the "ON CLICK" event.
Code:

<asp:ImageButtonID="ImageButton1"runat="server"onclick="ImageButton1.Click()"Style="z-index: 130; left: 465px;
position: absolute; top: 655px"ImageUrl="~/images/buttonSignUp.gif"/>
which is calling this bit of code:
Code:

PrivateSub ImageButton1_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
Try
'Declaration of all used variables
Dim SQLCommand AsNew Data.SqlClient.SqlCommand()
Dim SqlDataSource1 AsNew Data.SqlClient.SqlConnection("Data Source=SAMPLE;Initial Catalog=MMS;Persist Security Info=True;User ID=USER;Password=pas123")
'Parameters/values to be passed into SQL INSERT Command
SQLCommand.Parameters.Add("@One", SqlDbType.Char)
SQLCommand.Parameters(0).Value = txtFirst.Text
SQLCommand.Parameters.Add("@Two", SqlDbType.Char)
SQLCommand.Parameters(1).Value = txtLast.Text
SQLCommand.Parameters.Add("@Three", SqlDbType.Char)
SQLCommand.Parameters(2).Value = txtEmail.Text
SQLCommand.Parameters.Add("@Four", SqlDbType.Int)
SQLCommand.Parameters(3).Value = txtYear.Text
SQLCommand.Parameters.Add("@Five", SqlDbType.Char)
SQLCommand.Parameters(4).Value = rblGender.SelectedValue
SQLCommand.Parameters.Add("@Six", SqlDbType.Char)
SQLCommand.Parameters(5).Value = rblStudentType.SelectedValue
SQLCommand.Parameters.Add("@Seven", SqlDbType.Char)
SQLCommand.Parameters(6).Value = rblLiving.SelectedValue
SQLCommand.Parameters.Add("@Eight", SqlDbType.Int)
SQLCommand.Parameters(7).Value = dlInstitution.SelectedValue
SQLCommand.Parameters.Add("@Nine", SqlDbType.Char)
SQLCommand.Parameters(8).Value = rblReferredBy.SelectedValue
SQLCommand.Parameters.Add("@ten", SqlDbType.Char)
SQLCommand.Parameters(9).Value = txtOther.Text
'SQL Insert statement
SQLCommand.CommandText = "INSERT INTO StudentAssessment (FirstName, LastName, Email, BirthYear, Gender, StudentType, Housing, InstitutionID, ReferredByTypeID, RefByOtherDescription) VALUES & '(' & '@One' & '@Two' & '@Three' & '@Four' & '@Five' & '@Six' & '@Seven' & '@Eight' & '@Nine' & '@Ten' & ) "
SQLCommand.Connection = SqlDataSource1
SqlDataSource1.Open()
SQLCommand.ExecuteNonQuery()
SqlDataSource1.Close()
Catch ex As Exception
lblerr.Text = Err.Description()
EndTry
'Send user to Thanks page
Response.Redirect("ThankYou.aspx")
EndSub
Any thoughts on that?
__________________
EVERY DAY IS HALLOWEEN!
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
When You Register, These Ads Go Away!
     
Old 06-14-2007, 09:25 AM Re: ASP.NET: SQL INSERT (WTF?) and more...
mb2000inc's Avatar
Skilled Talker

Posts: 51
Name: Mark
Location: Ohio
And secondly, I can't get Javascript to work with "OnMouseover" and "OnMouseOut"
Code:
<a onmouseover="javascript:this.src='images/buttonwhaton.gif'; return true;"onmouseout="javascript:this.src='images/buttonWhat.gif'; return true;"href="faq.aspx#what"><img src='images/buttonWhat.gif' alt="What"style="border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none"/></a>
Any thoughts on that one?
__________________
EVERY DAY IS HALLOWEEN!

Last edited by mb2000inc : 06-14-2007 at 09:27 AM. Reason: terrible copy and paste and webastertalk is inserted in my code
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 06-14-2007, 04:27 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
I don't know about your javascript, but it looks like you're trying to use java script in place of .NET coding. Your imageButton1 object variable doesn't have a method called Click, which explains why you can't call it.

onclick="ImageButton1.Click()"
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 06-15-2007, 09:42 AM Re: ASP.NET: SQL INSERT (WTF?) and more...
mb2000inc's Avatar
Skilled Talker

Posts: 51
Name: Mark
Location: Ohio
Quote:
Originally Posted by Learning Newbie View Post
I don't know about your javascript, but it looks like you're trying to use java script in place of .NET coding. Your imageButton1 object variable doesn't have a method called Click, which explains why you can't call it.

onclick="ImageButton1.Click()"
Actually, you were looking at the second one... in the first one, I implemented the click method. And the second one is strictly an "href" event.
__________________
EVERY DAY IS HALLOWEEN!

Last edited by mb2000inc : 06-15-2007 at 09:45 AM. Reason: I can't type
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 06-15-2007, 11:58 AM Re: ASP.NET: SQL INSERT (WTF?) and more...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
Huh???

There is no click method. You can override OnClick, call it directly, or respond to the event it throws after registering for notifications to that event. But an event can't be implemented. Interfaces are implemented, so if you created one you could call it IClickable and then write a class - say a web form - that implements its OnClick method.

ImageButton1_Click != ImageButton1.Click()
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 06-15-2007, 03:53 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
mb2000inc's Avatar
Skilled Talker

Posts: 51
Name: Mark
Location: Ohio
sorry, I missunderstood where you were coming from. In the starting post at the top if the page there is this:
Code:
<asp:ImageButtonID="ImageButton1"runat="server"onclick="ImageButton1.Click()"Style="z-index: 130; left: 465px;
position: absolute; top: 655px"ImageUrl="~/images/buttonSignUp.gif"/>
So there is an click method...i guess.... I'm sorry for being a pain. I'm just really trying to understand what's happening with with it.
(yes, I'm an amatuer...as if you couldn't tell )
__________________
EVERY DAY IS HALLOWEEN!
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 06-15-2007, 03:56 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
mb2000inc's Avatar
Skilled Talker

Posts: 51
Name: Mark
Location: Ohio
This is the error I get when I do this:
Quote:
Server Error in '/WebProject' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC32022: 'Public Event Click(sender As Object, e As System.Web.UI.ImageClickEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

Source Error:

Line 107: top: 747px" Width="429px"></asp:Label>Line 108: Line 109: <asp:ImageButton ID="ImageButton1" runat="server" onclick="ImageButton1.Click()" Style="z-index: 120; left: 468px; position: absolute; top: 678px" ImageUrl="~/images/buttonSignUp.gif" />Line 110: Line 111: </form>
Source File: C:\WebProject\Default.aspx Line: 109
So, how do i fix it, and what is a "RaiseEvent"?
__________________
EVERY DAY IS HALLOWEEN!

Last edited by mb2000inc : 06-15-2007 at 03:58 PM. Reason: still can't type
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 06-15-2007, 04:09 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
There is no click method. There's a click event, but an event and a method are very different things. Ignore what it tells you about using RaiseEvent, because you want to respond to the event, not raise it. You want to be notified that something happened (in this case, a user clicked a button) instead of letting other code know that something happened.

In the designer, in design view, you should be able to double click the button, and Visual Studio is supposed to wire everything up for you. Try that, but if not, we'll go over the manual way, too.

You can create a method with the same calling signature (sender As Object, e As ImageClickEventArgs) and then attach the new method you create to the event itself.

In your VB code-behind, create a method called something like imageButton1_Click
(sender As Object, e As ImageClickEventArgs) and after the parameters, add Handles imageButton1.Click and/or set onclick to your new method that you just created in the html view. Because it gets run at the server, the compile will or at least should set up the plumbing behind the scenes for you.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 06-15-2007, 05:28 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
mb2000inc's Avatar
Skilled Talker

Posts: 51
Name: Mark
Location: Ohio
Ohhhhhhhhhhhhhhhhhhhhhhhhh!
I see! Lemme try that and I will get back to you. I won't be able to finish tonight, I will have to finish it tomorrow.

I will definitely give it a shot!
Thanks!

mb
__________________
EVERY DAY IS HALLOWEEN!
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 06-15-2007, 06:23 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
Yeah, no worries, but I probably won't be checking my email over the weekend.

Have you ever used C? A "delegate" in .net is like a function pointer. It describes a method you can call, and lets you do that in an anonymous, de-coupled way. The code in the ImageButton class under the covers uses a delegate aka a function pointer to call whatever method you wind up assigning to the event handler. That's beneath the sheets, though, at the level you can take control, what you need to know is a delegate ( which is how events work ) describes a method, but isn't one. You create a method that has the same signature, because that's how they talk, that's the API for responding to that event, and then you subscribe to notifications that the event has happened, and those notifications are relayed directly to your method, which saves you from writing code that says something like "If Message.ID = Messages.ImageButtonClickEvent Then DoThis()".

Now that we've cleared up some confusion, let's answer your question "So, how do i fix it, and what is a "RaiseEvent"?" because at this point it might help you (or someone else reading this) to understand events and how/when/why to respond to them, to understand how you would write code that raises an event.

The basic idea is you need to not break your code with all kinds of incestuous relationships where you have ten classes that can only work with all the other classes in your application, and aren't re-usable at all. Tightly coupled is bad. But you still might need to let other classes know something has happened, without knowing which classes you need to alert. The other option is usually you can write code to run a loop and keep asking a particular class if something happened, but that's really slow and bad code.

Class DirectoryWatcher
Dim Folder As String = "C:\Temp\"
Event FileAdded

Sub New()
Do
'Get a list of all the files in the directory, and then compare it to last time
If FileList.Count > oldFileCount Then RaiseEvent FileAdded
Loop
End Sub
End Class

Now that's oversimplified, and I haven't used VB.NET in a while, so I bet my syntax is off, in fact, I know it is. But that should give you the basic idea. It's not very efficient, and it would probably get run on a background thread, but it can tell other classes it doesn't know about when something happened - in this case, a file is added to a directory. Then other code in your application can get the notification so now it gets told when there's a change, and it can respond however it needs to.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 06-18-2007, 10:00 AM Re: ASP.NET: SQL INSERT (WTF?) and more...
mb2000inc's Avatar
Skilled Talker

Posts: 51
Name: Mark
Location: Ohio
Yeah, I didn't realize it was Friday, when I posted....I'm kind of a dork that way.

So, I tried something else this morning (I tried putting it directly into the HTML)... just on a whim...It's not exactly working, but you can probably guess where I'm going with it. My only issue, now is syntax...

Call me "A.D.D."

Code:
<asp:ImageButtonID="ImageButton1"runat="server"Style="z-index: 120; left: 468px; position: absolute; top: 678px"ImageUrl="~/images/buttonSignUp.gif"/>
<% Dim SQLCommand AsNew Data.SqlClient.SqlCommand()
Dim first, last, email, other, gender, type, living, refby AsString
Dim year, inst AsInteger
Dim SqlDataSource1 AsNew Data.SqlClient.SqlConnection("Data Source=Sample;Initial Catalog=MMS;Persist Security Info=True;User ID=US123;Password=NAsample")
 
SqlDataSource1.Open()
 
'Parameters/values to be passed into SQL INSERT Command
first = Request.Form("txtFirst.Text")
last = Request.Form("txtlast.Text")
email = Request.Form("txtemail.Text")
year = Request.Form("txtyear.Text")
other = Request.Form("txtother.Text")
gender = Request.Form("rblgender.SelectedValue")
type = Request.Form("rblStudentType.SelectedValue")
living = Request.Form("rblLiving.SelectedValue")
refby = Request.Form("rblReferredBy.SelectedValue")
inst = Request.Form("dlInstitution.SelectedValue")
 
'SQL INSERT Command
SQLCommand.CommandText = "INSERT INTO SampleTable (FirstName, LastName, Email, BirthYear, Gender, StudentType, Housing, InstitutionID, ReferredByTypeID, RefByOtherDescription) VALUES"
SQLCommand.CommandText = SQLCommand.CommandText "'(' & 'first ' & "," & 'last' & "," & 'email' & "," & 'year' & "," & 'gender' & "," & 'type' & "," & 'living' & "," & 'inst' & "," & 'refby' & "," & 'other' & ')"
SQLCommand.Connection = SqlDataSource1
'This is where the error is comming from:
SQLCommand.ExecuteNonQuery()
'This part is fine
SqlDataSource1.Close()
 
Response.Redirect("ThankYou.aspx")
 
%>
I really do appreciate your knowledge, you are truly gifted.
__________________
EVERY DAY IS HALLOWEEN!
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 06-18-2007, 12:15 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
What's wrong with the syntax? I'm not at the web dev machine, so I can't copy/paste it to see the error messages right now. I'm heading to a meeting soon, so if you want to paste the error(s) you're getting I'll be glad to take a look when I get back.

Here's a code change I would highly recommend.

Try
'SQL INSERT Command
SQLCommand.CommandText = "INSERT INTO SampleTable (FirstName, LastName, Email, BirthYear, Gender, StudentType, Housing, InstitutionID, ReferredByTypeID, RefByOtherDescription) VALUES"
SQLCommand.CommandText = SQLCommand.CommandText "'(' & 'first ' & "," & 'last' & "," & 'email' & "," & 'year' & "," & 'gender' & "," & 'type' & "," & 'living' & "," & 'inst' & "," & 'refby' & "," & 'other' & ')"
SQLCommand.Connection = SqlDataSource1
'This is where the error is comming from:
SQLCommand.ExecuteNonQuery()
'This part is fine
SqlDataSource1.Close()
Catch Ex As Exception
Response.Write("There was an error trying to run the query.<br />" + Ex.Message + "<br /><i>" + ex.StackTrace + "</i>")
Response.End
End Try

Without this extra code, your app will error out and give the user a nasty page they won't like,
if anyone enters a bad value in your form. Using a try block is like "On Error Goto X" in
VB 6 and lower. This will jump down to the "catch" block if an error is hit inside, and
continue running your code like it's okay.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 06-18-2007, 01:07 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
mb2000inc's Avatar
Skilled Talker

Posts: 51
Name: Mark
Location: Ohio
This is how it comes out.



In case you couldn't make out the small print, it said, "End Of Statment Expected". (if you want to see the full size image, you can do so here: http://i14.photobucket.com/albums/a3...l/errorlog.jpg)

What did I miss? Am I missing quotation marks, an apostrophe?, a parenthesis?

-stumped.
__________________
EVERY DAY IS HALLOWEEN!
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 06-18-2007, 01:17 PM Re: ASP.NET: SQL INSERT (WTF?) and more...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
You have:

SQLCommand.CommandText = SQLCommand.CommandText