Solved

Object reference not set to an instance of an object

Posted on 2011-09-05
3
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

690 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