Solved

Object reference not set to an instance of an object

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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