Subject: Accessing FormView fields
Posted By: LanceRoss Post Date: 7/25/2008 12:39:35 PM
I am a newbie.  I am writing my first ASP.NET 3.5 form in VB.
Does anyone know how to refer to the fields inside a FormView control that is in read only mode?  I have a SQL DB this control is pulling two fields from, firstname and lastname. I can't seem to find the syntax to get that data from the FormView to be able to use in a querystring to pass to the next page in the Code Behind of the Submit button.

Reply By: alliancejhall Reply Date: 7/25/2008 2:20:46 PM
use findcontrol....

Let's assume that your First and Last Names are in Labels in the FormView you can do
something like:

Dim FirstName As String = CType(FormView1.FindControl("FirstNameLabel"), Label).Text
Dim LastName As String = CType(FormView1.FindControl("LastNameLabel"), Label).Text

Now you have the first and last name in Variables that you can do anything with you would like.

Jason Hall
Reply By: LanceRoss Reply Date: 7/25/2008 2:27:06 PM
Thank you very much for the reply.  Now, if I wanted to use a Response.Redirect command to pass these variable to the next page, how would that syntax go?

Reply By: alliancejhall Reply Date: 7/25/2008 2:32:28 PM
easy...

Response.Redirect("~/Page.aspx?FN=" & FirstName & "&LN=" & LastName)

This isn't homework or something is it? I don't mind helping but can't ask us to do it for you.

Jason Hall
Reply By: LanceRoss Reply Date: 7/25/2008 2:39:36 PM
Thanks again.
No, not homework.  I am desperately trying to learn ASP.NET and VB in order to save my company money by developing our own webforms and not going to school (don't have the time, anyway).  I've read several of the Teach Yourself books, but need to fill in the gaps.  I am very grateful for your help.  On the Code Behind for the 2nd page, what would the syntax be to grab the query string data and post them to a label?

Reply By: alliancejhall Reply Date: 7/25/2008 2:43:26 PM
ok i know where your coming from i did the same thing so i'll be GLAD to help....

on the Page_Load (or any other event i guess) you would use

Dim FirstName As String = Request.QueryString("FN").ToString
Dim LastName As String = Request.QueryString("LN").ToString

now you have them in variable form to do what you like with in the code behind.

Jason Hall
Reply By: LanceRoss Reply Date: 7/25/2008 2:50:27 PM
Thank you so much Jason.  I'd don't feel so alone on this. Last two things:

If I have a label on the "Thank You for registering" page (the 2nd page that is being passed the two fields in the querystring) how do I assign it the value of either the new FirstName or LastName variables? See label control below.

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Lastly, what do you recommend that someone do to proceed in learning to be proficient in ASP.NET 3.5 and VB? Any suggestions would be greatly appreciated.

Reply By: alliancejhall Reply Date: 7/25/2008 2:56:47 PM
all you need to do is set the text value of the label to equal the variables...

Label1.Text = FirstName & " " & LastName

(notice i put a space between the first and last name " ")

What i recommend is just dive into it, learn the fundamentals and keep practicing (Everyday would be great) as often as you can. Look on these forums at other people's issues as you might learn something. When you need help supply as much code as relates to your issue that way it doesn't look like you are asking for people to do it for you, This also lets them critique what you have done and may offer a simpler/better way to do. Also don't take offense to what people say about your code because "Programmers" tell it like it is,  but, they are always willing to help. So just soak in as much as you can if this is what you really want to do.

Jason Hall
Reply By: LanceRoss Reply Date: 7/25/2008 4:27:54 PM
Thanks so much Jason.
If you have a chance, I need to figure out how to see if an ID is alread registered in my table and if so, to stop the INSERT and display an error.  Right now my app crashes on duplicates.  Here is my code for the DB call Sub:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim strConnectionString As String = ("server=SOLOMONSQL;Initial Catalog=RETracker;Persist Security Info=True;User ID=SA;Password=sa")
        Dim Conn As New SqlConnection(strConnectionString)
        Dim strSQL As String = "INSERT INTO tbl10K_Registrations (ID, Office, Mobile_Phone, Email_Address, Attending, Rep_Fname, Rep_Lname, Rep_Phone, Rep_Email) VALUES ('" + txtAdcode.Text + "', '" + txtOffice.Text + "', '" + txtMobile.Text + "', '" + txtEmail.Text + "', '" + RadioButtonList1.SelectedValue + "', '" + Rep_Fname.Text + "', '" + Rep_Lname.Text + "', '" + Rep_Phone.Text + "', '" + Rep_Email.Text + "')"
        Dim objCmd As New SqlCommand(strSQL, Conn)
        Conn.Open()
        objCmd.ExecuteNonQuery()
        Conn.Close()
        Dim FirstName As String = CType(FormView1.FindControl("AgentFirstNameLabel"), Label).Text
        Dim LastName As String = CType(FormView1.FindControl("AgentLastNameLabel"), Label).Text
        Response.Redirect("ThankYou.aspx?FN=" & FirstName & "&LN=" & LastName)
    End Sub

Reply By: alliancejhall Reply Date: 7/26/2008 12:32:40 PM
Ok sorry about taking so long.

What you can do is use an Exists statement in your SQL query:

"If Not Exists(Select ID from tbl10K_Registrations where ID='" & txtAdcode.Text & "') INSERT INTO tbl10K_Registrations (ID, Office, Mobile_Phone, Email_Address, Attending, Rep_Fname, Rep_Lname, Rep_Phone, Rep_Email) VALUES ('" + txtAdcode.Text + "', '" + txtOffice.Text + "', '" + txtMobile.Text + "', '" + txtEmail.Text + "', '" + RadioButtonList1.SelectedValue + "', '" + Rep_Fname.Text + "', '" + Rep_Lname.Text + "', '" + Rep_Phone.Text + "', '" + Rep_Email.Text + "')"

Jason Hall

Go to topic 72994

Return to index page 1