Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object. 1

Status
Not open for further replies.

sburden

Programmer
Dec 4, 2002
35
US
Hello:

I'm receiving the message, "Object reference not set to an instance of an object" when I run my program. I'm attempting to create passwords in the Web.Config file dynamically at runtime. My error occurs when it hits this line, "credentials.AppendChild(newElement)". Any help would be appreciated.


My code follows:


Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnLogin.Click
Dim strPlainText As String = txtPassword.Text
Dim strUsername As String = txtUserName.Text

If strPlainText <> "" And strUsername <> "" Then
Dim strHash As String = _
FormsAuthentication.HashPasswordForStoringInConfigFile( _
strPlainText, _
"sha1")

strPlainText = ""

'**********************************************
'Open config file and build new password digest
'**********************************************
Dim doc As New XmlDocument

doc.Load(Server.MapPath("..\Faculty\Web.config"))

Dim newElement As XmlElement = doc.CreateElement("user")

Dim attribName As XmlAttribute = doc.CreateAttribute("name")
attribName.Value = strUsername

Dim attribPassword As XmlAttribute = doc.CreateAttribute("password")
attribPassword.Value = strHash

newElement.Attributes.Append(attribName)
newElement.Attributes.Append(attribPassword)

Dim credentials As XmlElement = doc.GetElementsByTagName("credentials").Item(0)

credentials.AppendChild(newElement)

doc.Save(Server.MapPath("../Faculty/Web.config"))

lblResults.Text = "User saved. Username:" & _
strUsername & " Password:" & strHash

lblResults.Visible = True

txtPassword.Text = ""
txtUserName.Text = ""
End If

If FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text) Then
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkAutomatic.Checked)
Else
lblError.Text = "Incorrect user name or password. Please try again."
End If

End Sub


 
change this
Code:
Dim doc As New XmlDocument
to this
Code:
Dim doc As XmlDocument = New XmlDocument()

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Thanks checkai for responding however, I put in the change and getting the same message.
 
and what value do you have just before that for this element.

doc.GetElementsByTagName("credentials").Item(0)

perhaps credentials = nothing

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thanks chrissie1 for pointing me in the right direction. I forgot to add the credential element to the Web.config file.
 
Was going to start a new thread but since I am having trouble with the same error message as this one I thought I'd attach it here. I am getting an "object reference not set to instance.." error for a dropdownlist in my update Datagrid.

I have been struggling with the Edit/Update Datagrid and now putting a DropdownList inside it (Edit Template) with a label showing the value for the non-editing grid.

For the following code on Edit:
Code:
Sub Grid_Edit(ByVal Sender As Object, ByVal E As System.Web.UI.WebControls.DataGridCommandEventArgs)
         strAtt1 = CType(e.Item.FindControl("lblAtt1"), Label).Text
         
         Dim dded As DropDownList = New DropdownList()
         dded = e.Item.FindControl("ddAtt1")

         If strAtt1 = "True" Then
           dded.SelectedItem.Value = "01"
         Else
           dded.SelectedItem.Value = "02"
         End If

         If Not isEditing Then
           DataGrid1.EditItemIndex = e.Item.ItemIndex
           BindGrid()
         End If
    End Sub

I get the Error message that is the title of this thread, i.e., not set to instance of the object. The string "strAtt1" is behaving correctly and the break line for the error is inside the IF..THEN statement when a Value is assigned to the dropdown 'dded', which is what causes the error.

For clarity my template column on the aspx page is as follows:
Code:
<asp:TemplateColumn HeaderText="Att1">
 <ItemTemplate>
  <asp:Label id=lblAtt1 runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Att1") %>'/>
 </ItemTemplate>	      
 <EditItemTemplate>
    <asp:DropDownList id="ddAtt1" runat="server">
      <asp:ListItem Value="01">Yes</asp:ListItem>
      <asp:ListItem Value="02">No</asp:ListItem>
    </asp:DropDownList>
 </EditItemTemplate>
</asp:Templatecolumn>

I hope you don't mind me appending this thread but since it was the same Error message I thought perhaps it might be appropriate to do so.
 
Try for kicks setting the dded.SelectedIndex to something different to see if you can even set the control at this point.

I believe the controls for this purpose are read only.
If you want to set the dded value use the DataGrid_ItemBound event. This is where you manipulate controls in a datagrid.
 
RT - thanks. That is what I am testing now; to pre-set the value in the ItemDataBound event - will give it a shot; I've come across a few solutions to setting the edit item dropdownlist and then updating the new value if changed -- thanks.
 
Just check so that you are not looking at footer and headers:

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound


If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
strAtt1 = CType(e.Item.FindControl("lblAtt1"), Label).Text

Dim dded As DropDownList
dded = e.Item.FindControl("ddAtt1")

If strAtt1 = "True" Then
dded.SelectedItem.Value = "01"
Else
dded.SelectedItem.Value = "02"
End If

End If
End Sub
 
Thanks chrissie1 for pointing me in the right direction. I forgot to add the credential element to the Web.config file.
Whenever you do an XPath statement (like getting attributes and elements, even if it's under the hood) your code should always assign the result to a variable, and then check that varible for null/Nothing-ness. Just good defensive programming practice. You never know when a user will munge up a configuration file.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top