Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

send form values-basic question 2

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,
This is first time i am trying to create a application in .net (using my asp logic)

OK..i have a created a form...lets say with two text fields and a submit button...lets say this default.aspx.here is how i have the code...showing only the relevant parts
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Default.aspx.vb" Inherits="olpat._Default"%>

<form id="announce" action="process.aspx" method="post" runat="server">
<asp:textbox id="FName" runat="server"></asp:textbox>
<asp:textbox id="LName" runat="server"></asp:textbox>
<asp:button id="Announce" runat="server" Text="Announce Name"></asp:button>
</form>

ok now...as you can see i have said method="post" action="process.aspx"

now how can i grab these values in the process.aspx page and display the name or doing some insert statement...

let me know if you need more info...

Thanks...

-DNG

 
I would remove the action part. Then in the onclick, you can do a Server.Transfer to your page. In that page you can reference the objects.

Ex: on your process.aspx page:

Response.Write(Request.Form("FName").ToString)

Jim
 
In the page load event of process.aspx :

Code:
   string FName =  Request.QueryString["FName"];
   string LName =  Request.QueryString["LName"];
...

hope this helps,
jermine
 
ok guys...i did something like this:

i removed the action part from my form...

in the code behind...for the button click sub i wrote this line:

Server.Transfer("process.aspx")

ok now on the process.aspx page...i dont know what to write...but in the code behind(process.aspx.vb) i wrote the following...
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       InsertIntoDb()
            End Sub
Code:
Private Sub InsertIntoDb()

        Dim sConnection As String
        Dim strSQL As String
        Dim dbConn As OleDb.OleDbConnection
        Dim dbCmd As OleDb.OleDbCommand

        Dim FName As String = Replace(Request.Form("fname"), "'", "''")
        Dim LName As String = Replace(Request.Form("lname"), "'", "''")

        sConnection = ConfigurationSettings.AppSettings("olpat")

        strSQL = "INSERT INTO Mytable(FirstName,LastName)VALUES('"&FName&"','"&LName&"')"

        dbConn = New OleDb.OleDbConnection(sConnection)
        dbConn.Open()

        dbCmd = New OleDb.OleDbCommand(strSQL, dbConn)
        dbCmd.ExecuteNonQuery()
        dbConn.Close()

    End Sub

is that correct? i dont know where to code a confirmation part...a javascript window saying record added and send them back to the form page...or someother confirmation page...

and i havent coded anything in the process.aspx page?? i am just trying to understand the whole structure...

thanks for your replies...

-DNG
 
You could do it this way, the traditional ASP way. You can also do the insert on the original page where they entered the data. You can then just hide the inputs and button on the click of the button, with a message that says, update successful, or an error message.
I usually add a label to the page with a multiline textbox and make them invisible. Then I do my DB actions in a try catch. If there is an error, I display it in the textbox with the label, stating "An Error Occured". If it is successful, I just make the label visible stating, either "Insert Succesful" or "Update Successful"

Jim
 
Jim,

So you say that my above code is correct...if so i want to just follow this route and can you show me how i can how can i pop up the javascript window when the insert is successful...

Thanks

-DNG
 
Why do a javacript pop-up? You can display the message right on your page.
 
then you want to put the link back to the main page??

or can i do a response.redirect??

i want them to go back to the main page after confirmation page...

-DNG
 
Like I said. This would all be easier to do it you just stay on the same page, and show/hide objects as needed.
You can display a message then redirect, the problem is, it will happen so fast that they won't see the message.
Showing a pop-up just adds a whole other piece of complexity I am not sure even will work.
 
cant i a put a hyper link saying "back to main page"(which i make it visible) after the confirmation message is shown??

or may i will just stay on the same page and toggle between making fields visible and invisible...

Thanks

-DNG

 
As I said it is just a matter of preference. Doing a confirmation page (separatley) is the traditional way of doing things. It can still be done that way. Your idea of showing the hyperlink after confirmation is fine. I would put it in a Try..Catch block and only show if there are no errors.
 
Sorry if I've misunderstood what you want to do but why do you need to go back to the other page? Don't you want to do something like this:
Code:
    Protected Sub btnAnnounce_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAnnounce.Click
        If myFunction(FName.Text, LName.Text) = True Then
            ' Everything worked so show confirmation message and/or hide ertain sections
            lblConfirmation.Text = "Well Done!"
        Else
            ' An error occurrec so show message and/or hide certain sections
            lblConfirmation.Text = "Something went wrong!"
        End If
    End Sub

    Private Function myFunction(ByVal Forename As String, ByVal Lastname As String) As Boolean
        ' Code goes here to return true or false based on your logic
    End Function


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
guys, is this syntax correct??

Dim FName As String = Replace(Request.Form("fname"), "'", "''")
Dim LName As String = Replace(Request.Form("lname"), "'", "''")

strSQL = "INSERT INTO Mytable(FirstName,LastName)VALUES([red]'"&FName&"','"&LName&"'[/red])"

the editor is showing it as a syntax error...

Thanks

-DNG
 
hmm...i guess i need to use the + signs...

Thanks

-DNG
 
Try adding some spaces between the variables e.g.
Code:
strSQL = "INSERT INTO Mytable(FirstName,LastName) VALUES ('" & FName & "','" & LName & "')"


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
yep that worked...thanks guys...

how do i split the sql statement into multiple lines...

i tries...

strSQL="insert into mytable(field1,field2)"&_
" values...."

it did not like that...is there anything wrong with that...

-DNG
 
You just need a space again (befoe your & sign)
Code:
        strSQL = "insert into mytable(field1,field2)" & _
        " values...."


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

that worked...thanks so much..one last question related to this...

My code is not working...i am getting "System.NullReferenceException: Object reference not set to an instance of an object."

Can you please go thru it...it shud be very simple for you...

here is my code in simplified terms...

default.aspx
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Default.aspx.vb" Inherits="olpat._Default"%>

<form id="frmannounce" method="post" runat="server">
<asp:textbox id="Fname" runat="server"></asp:textbox>
<asp:button id="submitButton" runat="server" Text="Submit"></asp:button>
</form>

default.aspx.vb(codebehind)
Code:
Private Sub submitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnnounceButton.Click
        Server.Transfer("process.aspx")
    End Sub

process.aspx
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="processannouncement.aspx.vb" Inherits="olpat.processannouncement"%>
<form id="Form1" method="post" runat="server">
<asp:Label id="lblConfirmation" runat="server">Label</asp:Label>
<asp:TextBox id="Errorbox" runat="server" TextMode="MultiLine"></asp:TextBox>
</form>

process.aspx.vb(codebehind)
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InsertIntoDb()
    End Sub

 Private Sub InsertIntoDb()

        Dim sConnection As String
        Dim strSQL As String
        Dim dbConn As OleDb.OleDbConnection
        Dim dbCmd As OleDb.OleDbCommand
        Try

        Dim Fname As String = Replace(Request.Form("Fname"), "'", "''")

            sConnection = ConfigurationSettings.AppSettings("olpat")

            strSQL = " INSERT INTO mytable(Myname)" &_
		     " VALUES ('" & fname & "') "

            dbConn = New OleDb.OleDbConnection(sConnection)
            dbConn.Open()

            dbCmd = New OleDb.OleDbCommand(strSQL, dbConn)
            dbCmd.ExecuteNonQuery()
            Errorbox.Visible = False
            lblConfirmation.Visible = False

        Catch ex As Exception
            Errorbox.Text = ex.ToString & "<br>"
            Errorbox.Visible = True
        Finally
            lblConfirmation.Text = "Thanks for submitting your name"
            lblConfirmation.Visible = True
      Response.Write("<a href=""[URL unfurl="true"]http://myservername/blah"">Go[/URL] Back</a>")
            dbConn.Close()
        End Try

    End Sub

where did i go wrong...please suggest...Thanks in advance...

-DNG
 
Which line did you get that error on?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top