?
Solved

Object reference not set to an instance of an object

Posted on 2011-09-05
3
Medium Priority
?
311 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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