locked
Object not declared error RRS feed

  • Question

  • User-1636525995 posted

    Hi, i have formview control in my page and on that i have two textboxes and button in it which display at runtime. i declare the button event in my aspx.vb

    Protected Sub insertbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If Not Page.IsValid Then Exit Sub

            ' Determine the currently logged on user's UserId
            Dim currentUser As MembershipUser = Membership.GetUser()
            Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)

            Dim connectionString As String = ConfigurationManager.ConnectionStrings("SecurityTutorialsConnectionString").ConnectionString
            Dim insertSql As String = "INSERT INTO Reply(Subjects, Bodi, CommentDates UserId) VALUES(@Gameid, @Subjects, @Bodi, @CommentDates, @UserId)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()

                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@Subject", subject1.Text.Trim())
                myCommand.Parameters.AddWithValue("@message", message.Text.Trim())
                myCommand.Parameters.AddWithValue("@CommentDates", DateTime.Now())
                myCommand.Parameters.AddWithValue("@UserId", currentUserId)
                myCommand.ExecuteNonQuery()
                Label2.Text = "Your Comment has Been Added to our Database. Thank You"
                myConnection.Close()
            End Using

        End Sub

    and the button property

    <asp:Button ID="Button1" runat="server" Text="post" CausesValidation="true" OnClick="insertbutton_Click" />

    but when i click on button it shows the error like this.

     

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30451: Name 'subject1' is not declared.

    Source Error:


    Line 39: 
    Line 40: Dim myCommand As New SqlCommand(insertSql, myConnection)
    Line 41: myCommand.Parameters.AddWithValue("@Subject", subject1.Text.Trim())
    Line 42: myCommand.Parameters.AddWithValue("@message", message.Text.Trim())
    Line 43: myCommand.Parameters.AddWithValue("@CommentDates", DateTime.Now())

    i place the following in my  onload event

    Dim drp As Label
            drp = form1.FindControl("Label2")


            Dim erp As TextBox
            erp = form1.FindControl("subject1")


            Dim crp As TextBox
            crp = form1.FindControl("message")

    but the object not handled at runtime.

    Please help me to solve this.

    Thank you 

    Sunday, July 6, 2008 7:17 AM

Answers

  • User187056398 posted

    Yes, i have textbox named subject1 and other objects in my formview control.

    Ok, the controls are in the FormView and NOT the Form.

    You can try something like this to get at them:

    Dim TB2 As TextBox = TryCast(FormView1.FindControl("TextBox2"), TextBox)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 6, 2008 11:03 AM
  • User187056398 posted

    You put it where you need to access the TextBox.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 6, 2008 11:40 AM

All replies

  • User187056398 posted

    Do you have a TextBox named subject1 on your form?

    Sunday, July 6, 2008 10:23 AM
  • User-1636525995 posted

     Yes, i have textbox named subject1 and other objects in my formview control. i can show it below:

     <asp:TextBox ID="subject1" runat="server" Text='<%# Eval("Subject") %>' />

    My Code view:

    Protected Sub insertbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If Not Page.IsValid Then Exit Sub

            ' Determine the currently logged on user's UserId
            Dim currentUser As MembershipUser = Membership.GetUser()
            Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)

            Dim connectionString As String = ConfigurationManager.ConnectionStrings("SecurityTutorialsConnectionString").ConnectionString
            Dim insertSql As String = "INSERT INTO Reply(Subjects, message, CommentDates UserId) VALUES(@Subjects, @message, @CommentDates, @UserId)"

            Using myConnection As New SqlConnection(connectionString)
                myConnection.Open()

                Dim myCommand As New SqlCommand(insertSql, myConnection)
                myCommand.Parameters.AddWithValue("@Subject", subject1.Text.Trim())
                myCommand.Parameters.AddWithValue("@message", message.Text.Trim())
                myCommand.Parameters.AddWithValue("@CommentDates", DateTime.Now())
                myCommand.Parameters.AddWithValue("@UserId", currentUserId)
                myCommand.ExecuteNonQuery()
                Label2.Text = "Your Comment has Been Added to our Database. Thank You"
                myConnection.Close()
            End Using

    at runtime it display object not found or a withevents clause required for object.

    please help me to solve this. 

    Thank you 

    Sunday, July 6, 2008 10:44 AM
  • User187056398 posted

    Yes, i have textbox named subject1 and other objects in my formview control.

    Ok, the controls are in the FormView and NOT the Form.

    You can try something like this to get at them:

    Dim TB2 As TextBox = TryCast(FormView1.FindControl("TextBox2"), TextBox)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 6, 2008 11:03 AM
  • User-1636525995 posted

    Thank you sir for your reply, but i want to know that the code should i put onload event or itemupdating event.

    Thank you 

    Sunday, July 6, 2008 11:16 AM
  • User187056398 posted

    You put it where you need to access the TextBox.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 6, 2008 11:40 AM
  • User-1636525995 posted

    I put the code but the error is same as previous.

    Dim TB2 As TextBox = TryCast(form1.FindControl("subject1"), TextBox) 

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30451: Name 'subject1' is not declared.

    Source Error:


    Line 34: 
    Line 35: Dim myCommand As New SqlCommand(insertSql, myConnection)
    Line 36: myCommand.Parameters.AddWithValue("@Subject", subject1.Text.Trim())
    Line 37: myCommand.Parameters.AddWithValue("@message", message.Text.Trim())
    Line 38: myCommand.Parameters.AddWithValue("@CommentDates", DateTime.Now())
     

    My formview control is shown below: 

    <asp:FormView ID="forms1" runat="server" DataSourceID="BookSource">
      <ItemTemplate>
      <asp:Panel ID="panel1" runat="server">
          <asp:Label ID="Label2" runat="server" Text="Reply to an existing Message"></asp:Label>
      </asp:Panel><br />
      <asp:Panel ID="panel2" runat="server">
      The message you are replying to: <asp:Label ID="text1" runat="server" Text='<%# Eval("Subject") %>' />
      </asp:Panel><br />
      <asp:Panel ID="panel3" runat="server">
      <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />wrote the following post at <asp:Label ID="CommentDateLabel" runat="server" Text='<%# Eval("Commentdate") %>' />
      <table>
      <tr>
      <td style="background-color:Gray; color:Black">
      <asp:Label ID="Label1" runat="server" Text='<%# Eval("Body") %>' />
      </td>
      </tr>
      <tr>
      <td>
      Subject
      </td>
      </tr>
      <tr>
      <td>
      <asp:TextBox ID="subject1" runat="server" Text='<%# Eval("Subject") %>' />
      </td>
      </tr>
      <tr>
      <td>
      Message    
      </td>
      </tr>
      <tr>
      <td>
      <asp:TextBox id="message" runat="server" TextMode="MultiLine" />
      </td>
      </tr>
      <tr>
      <td>
      <asp:Button ID="Button1" runat="server" Text="post" CausesValidation="true" OnClick="insertbutton_Click" />
      </td>
      </tr>
      </table>
      </asp:Panel>

      </ItemTemplate>
      </asp:FormView>        

    Thank you 

    Sunday, July 6, 2008 1:20 PM
  • User187056398 posted

    It should be like this (you may want to give it a better name):

    myCommand.Parameters.AddWithValue("@Subject", TB2.Text.Trim())

    Sunday, July 6, 2008 5:24 PM
  • User-1636525995 posted
    Hi, i got an error at designtime of my page that on top

    <%@ Page Language="VB" MasterPageFile="~/Masters.master" AutoEventWireup="false" CodeFile="Reply.aspx.vb" Inherits="Reply" title="Untitled Page" %>

    their is blue wavy line and displaying error on mouse over: ASP.NET Runtime error: Child Nodes not allowed. in webconfig line 150)

    and line 150 is:

    <system.codedom>

    <compilers>

    (*150) <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">

    <providerOption name="CompilerVersion" value="v3.5"/>

    <providerOption name="WarnAsError" value="false" /></compiler>

    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">

    <providerOption name="CompilerVersion" value="v3.5"/>

    <providerOption name="OptionInfer" value="true"/>

    <providerOption name="WarnAsError" value="false"/></compiler></compilers></system.codedom>

    please help me to solve this.

     

    Tuesday, July 8, 2008 2:58 AM
  • User-1636525995 posted

    Another error occurs at runtime that:

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 3:  Partial Class Reply
    Line 4:      Inherits System.Web.UI.Page
    Line 5:      Dim TB2 As TextBox = TryCast(Form1.FindControl("TB2"), TextBox)
    Line 6:      Dim LBL As Label = TryCast(form1.FindControl("LBL"), Label)
    Line 7:      Dim MSG As TextBox = TryCast(form1.FindControl("MSG"), TextBox)

    thank you

    Tuesday, July 8, 2008 3:12 AM
  • User-772658648 posted

     make sure that the current user is logged in or authenticated before the could add comments. 

    i think the  currentUser is null so your getting that error

    place your guestbook.aspx in a member only area or folder so only those who are logged in or authenticated could access it.

    or

    you may add this code to the page load event of your guestbook.aspx

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         If User.Identity.IsAuthenticated = False Then
                Response.Redirect("login.aspx")
          End If
    End Sub


    Thursday, July 10, 2008 8:44 PM