Solved

Object reference not set to an instance of an object

Posted on 2011-09-05
3
303 Views
Last Modified: 2012-06-27
Getting a Object reference not set to an instance of an object when submitting the below code.


<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="utf-8" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ASP.NET Purl</title>

<script runat="server">
  Sub Page_Load
  'GET CONNECTION DATABASE TO ESTABLISH SESSION VARIABLES
    Dim objConn As SqlConnection
    Dim cmdSelect As SqlCommand
    Dim objRS As SqlDataReader
      
   objConn = New SqlConnection(ConfigurationManager.ConnectionStrings("MainConnStr").ConnectionString)
   objConn.Open()
   cmdSelect = New SqlCommand(  "Select * From TableName WHERE Purl = 'www.domainname.com/STGray'", objConn )
   objRS = cmdSelect.ExecuteReader()
    FormView1.DataSource = objRS
    FormView1.DataBind()
 
  End Sub
 
 
Sub SubmitForm(Src As Object, E As EventArgs)
 ' If Page.IsValid
  Dim objConn As SqlConnection
  Dim sSQL As String
  Dim cmdUpdate As SqlCommand
  Dim txtfname As TextBox = FindControl("txtFname")
 
  objConn = New SqlConnection(ConfigurationManager.ConnectionStrings("MainConnStr").ConnectionString)
  sSQL = "UPDATE TableName SET Fname = @Fname WHERE AutoID = '1'"
  cmdUpdate = New SqlCommand( sSQL, objConn )
  cmdUpdate.Parameters.Add( "@Fname", txtfname.Text )
  objConn.Open()
  cmdUpdate.ExecuteNonQuery()
  objConn.Close()
  'Response.Redirect("http://www.kelmscottpurls.com")
 
 'End IF
End Sub
</script>

</head>

<body>

<!-- COPY PASTE AND MODIFY FORM INFO  -->
 

<form runat="server">
<asp:FormView ID="FormView1" runat="server" DefaultMode="Edit">
            <EditItemTemplate>

<!-- CONTACT INFORMATION TO BE PLACED HERE  -->
 <table border="0" width="600" cellspacing="5" cellpadding="0" class="txtForm">
    <tr>
      <td align="left" valign="top" width="600" colspan="2"><b></b>, welcome to your personalized url...........</td>
      </tr>
      <tr>
          <td align="left" valign="top" width="600" colspan="2">&nbsp;</td>
      </tr>
      <tr>
      <td align="left" valign="top" width="600" colspan="2"><span class="asterisk">* </span> <b>Denotes required field</b></td>
       </tr>
     <!-- AREAS OF INTEREST  -->
   
     <tr>
      <td align="right" valign="middle" width="200"><span class="asterisk">* </span> First Name</td>
      <td align="left" valign="top" width="400"><asp:TextBox id="txtfname" text='<%# Eval("Fname") %>' columns="30" runat="server" />&nbsp;
        <asp:RequiredFieldValidator ControlToValidate="txtfname" Text="First name is required!" runat="server" /></td>
     </tr>
 
    <tr>
     <td colspan="2" align="right" valign="top"><asp:Button id="b1" Text="Submit" runat="server" onclick="SubmitForm" style="background:#c8ba89;color:#000000" /></td>
    </tr>
   </table>
      </EditItemTemplate>
</asp:FormView>
      </form>


<!-- END COPY AND PASTE FORM INFO  -->       
     
     
      <!-- FOOTER AND GOOGLE INFO  -->
        

      


</body>
</html>
0
Comment
Question by:1665kelmscott
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
himanshut earned 125 total points
ID: 36486442
Your textbox ID is txtfname and you are finding FindControl("txtFname")

it should be FindControl("txtfname") isn't it?

0
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36486573
Hi! Besides FindContol searches for first level children of the Page (not children on children). So it is better to use recursive search function:
Private Function FindControlRecursive(
    ByVal rootControl As Control, ByVal controlID As String) As Control

    If rootControl.ID = controlID Then
        Return rootControl
    End If

    For Each controlToSearch As Control In rootControl.Controls
        Dim controlToReturn As Control =
            FindControlRecursive(controlToSearch, controlID)
        If controlToReturn IsNot Nothing Then
            Return controlToReturn
        End If
    Next
    Return Nothing
End Function

Dim txtfname As TextBox = FindControl(Me, "txtfname")
0
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36486576
Sorry: Dim txtfname As TextBox = FindControlRecursive(Me, "txtfname")
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 74
Set cookies HttpOnly and Secure 4 107
ASP.NET Web Form Variables being reset 2 25
Convert to Hour/minute textbox and compare 3 52
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now