Solved

Object reference not set to an instance of an object

Posted on 2011-09-05
3
302 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

11 Experts available now in Live!

Get 1:1 Help Now