System.NullReferenceException on remote Machine? - asp.net

I have created a web site with window form, I have an error of System.NullReferenceException only in remote page, if I try to open the page with visual studio, in debug mode, there is not problems! WHY!!?!?!?!
[NullReferenceException: Riferimento a un oggetto non impostato su un'istanza di oggetto.]
GeCo.DetaglioMilitare.Page_Load(Object sender, EventArgs e) in C:\Users\j972537\Documents\
Visual Studio 2010\Projects\GeCo\Admin\DettaglioMilitare.aspx.vb:45
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(
Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
this is the code with the error:
Dim militare As New ldap.utente
militare = ldap.ldap_utente(matricola2)
Label7.Text = militare.grado
Label8.Text = militare.cognome
Label9.Text = militare.nome
Label10.Text = militare.codice_reparto
Label11.Text = militare.intestazione_reparto
Label12.Text = militare.email
this is the class module that I created:
Public Class ldap
Public Class utente
Private _matricola As String = ""
Public Property matricola() As String
Get
Return _matricola
End Get
Set(ByVal val As String)
_matricola = val
End Set
End Property
Private _grado As String = ""
Public Property grado() As String
Get
Return _grado
End Get
Set(ByVal val As String)
_grado = val
End Set
End Property
Private _cognome As String = ""
Public Property cognome() As String
Get
Return _cognome
End Get
Set(ByVal val As String)
_cognome = val
End Set
End Property
Private _nome As String = ""
Public Property nome() As String
Get
Return _nome
End Get
Set(ByVal val As String)
_nome = val
End Set
End Property
Private _codice_reparto As String = ""
Public Property codice_reparto() As String
Get
Return _codice_reparto
End Get
Set(ByVal val As String)
_codice_reparto = val
End Set
End Property
Private _intestazione_reparto As String = ""
Public Property intestazione_reparto() As String
Get
Return _intestazione_reparto
End Get
Set(ByVal val As String)
_intestazione_reparto = val
End Set
End Property
Private _email As String = ""
Public Property email() As String
Get
Return _email
End Get
Set(ByVal val As String)
_email = val
End Set
End Property
End Class
' funzione per estrarre i dati relativi ad una matricola
Public Shared Function ldap_utente(matricola As String) As utente
Dim directory As DirectoryServices.DirectorySearcher
Dim result As DirectoryServices.SearchResult
Dim militare As utente = New utente
Try
directory = New DirectoryServices.DirectorySearcher("(cn=" & matricola & ")")
result = directory.FindOne
militare.matricola = matricola
militare.grado = result.Properties("title")(0).ToString
militare.cognome = result.Properties("sn")(0).ToString
militare.nome = result.Properties("givenname")(0).ToString
militare.codice_reparto = Left(result.Properties("physicaldeliveryofficename")(0).ToString, 5)
Dim lenght_string As Integer = result.Properties("description")(0).ToString.Length
militare.intestazione_reparto = Trim(Right(result.Properties("description")(0).ToString, lenght_string - 7))
militare.email = result.Properties("mail")(0).ToString()
Return militare
Catch ex As Exception
Return Nothing
End Try
End Function

Because you did not include information which line is DettaglioMilitare.aspx.vb:45, I can only diagnose what probably happened.
In you function ldap_utente when wxception happened you return Nothing which is same as Null. So to investigate what is wrong in your code you have two options:
Debug your code to see what exception you got
If debugging is not possible add logging to your code or throw this exception to see it in browser window\
I am almost sure that you have problem with connecting to Active Directory, because in 99% your page is installed as IIS user which doesn't have access to Active Directory. You can try to change application pool user to domain user.

Related

Not getting Custom Error Message in REST

I am developing a REST API.
Below is my code:
In IService.vb
<OperationContract(),
WebGet(UriTemplate:="/DCU/{ClientID}",
RequestFormat:=WebMessageFormat.Xml,
ResponseFormat:=WebMessageFormat.Xml,
BodyStyle:=WebMessageBodyStyle.Bare)>
Function GetAllData(ByVal ClientID As String) As List(Of Data)
In Service.vb
Friend Class Service
Implements IService
Public Function GetAllData(ByVal intClientID As String) As System.Collections.Generic.List(Of Data) Implements ILightingGaleService.GetAllDCUs
Dim lstdata As New List(Of Data)()
If IsNumeric(intClientID) Then
Else
Throw New FaultException("Invalid Client ID")
End If
End Class
In Web.Config file
<serviceDebug includeExceptionDetailInFaults="true"/>
I am getting
Request Error:The server encountered an error processing the request. See server logs for more details.
When I am passing a String value in Client ID. Though I have implemented FaultExpcetion i never get "Invalid Client ID" message in POSTMAN or Web Client.
Can anyone help me on how can I get my custom message on POSTMAN or web client?
I was able to resolve this by implementing below:
I generated a class CustomError:
<DataContract> _
Public Class CustomError
Public Sub New(strError As String, strErrorDesc As String, strErrorCode As Long)
ErrorInfo = strError
ErrorDetails = strErrorDesc
ErroCode = strErrorCode
End Sub
<DataMember> _
Public Property ErrorInfo() As String
Get
Return m_ErrorInfo
End Get
Private Set(value As String)
m_ErrorInfo = value
End Set
End Property
Private m_ErrorInfo As String
<DataMember> _
Public Property ErrorDetails() As String
Get
Return m_ErrorDetails
End Get
Private Set(value As String)
m_ErrorDetails = value
End Set
End Property
Private m_ErrorDetails As String
<DataMember> _
Public Property ErroCode() As Long
Get
Return m_ErroCode
End Get
Private Set(value As Long)
m_ErroCode = value
End Set
End Property
Private m_ErroCode As Long
End Class
In Service.vb
Friend Class Service
Implements IService
Public Function GetAllData(ByVal intClientID As String) As System.Collections.Generic.List(Of Data) Implements ILightingGaleService.GetAllDCUs
Dim lstdata As New List(Of Data)()
If IsNumeric(intClientID) Then
Else
Dim ExceptionError As New CustomError("Client ID not found.", "The Client ID is not found in system.", HttpStatusCode.NotFound)
Throw New WebFaultException(Of CustomError)(ExceptionError, HttpStatusCode.NotFound)
End If
End Class
So this will generate a Webexception for my Custom Error Message which I wanted to display on my Client Application:
Now on my client application I used below
Catch ex As WebException
Dim strError = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
lblErrorMsg.Text = strError
End Try
Hope this helps to someone.

asp.net web service error on production and not locally

Hope i am posting in the right area!
I have an asp.net/vb web service, built in VS2012. The project is running locally under .net 4.0 but the project was created to use .net 2.0 as that is what the production server runs.
The web service runs fine locally, but on the production server, I get an error (i hope the formatting works):-
BC30068: Expression is a value and therefore cannot be the target of an assignment.
This is my function and properties:-
Public Class JobData
Private m_ID As Integer
Public Property ID() As Integer
Get
Return m_ID
End Get
Set(value As Integer)
m_ID = value
End Set
End Property
Private m_MAID As Integer
Public Property MAID() As Integer
Get
Return m_MAID
End Get
Set(value As Integer)
m_MAID = value
End Set
End Property
Private m_DateAdded As DateTime
Public Property DateAdded() As DateTime
Get
Return m_DateAdded
End Get
Set(value As DateTime)
m_DateAdded = value
End Set
End Property
Private m_ClientID As Integer
Public Property ClientID() As Integer
Get
Return m_ClientID
End Get
Set(value As Integer)
m_ClientID = value
End Set
End Property
Private m_JobRef As String
Public Property JobRef() As String
Get
Return m_JobRef
End Get
Set(value As String)
m_JobRef = value
End Set
End Property
Private m_JobTitle As String
Public Property JobTitle() As String
Get
Return m_JobTitle
End Get
Set(value As String)
m_JobTitle = value
End Set
End Property
Private m_Location As String
Public Property Location() As String
Get
Return m_Location
End Get
Set(value As String)
m_Location = value
End Set
End Property
Private m_JobType As String
Public Property JobType() As String
Get
Return m_JobType
End Get
Set(value As String)
m_JobType = value
End Set
End Property
Private m_Salary As String
Public Property Salary() As String
Get
Return m_Salary
End Get
Set(value As String)
m_Salary = value
End Set
End Property
Private m_SalaryTerm As String
Public Property SalaryTerm() As String
Get
Return m_SalaryTerm
End Get
Set(value As String)
m_SalaryTerm = value
End Set
End Property
Private m_BriefDescription As String
Public Property BriefDescription() As String
Get
Return m_BriefDescription
End Get
Set(value As String)
m_BriefDescription = value
End Set
End Property
Private m_MoreDetails As String
Public Property MoreDetails() As String
Get
Return m_MoreDetails
End Get
Set(value As String)
m_MoreDetails = value
End Set
End Property
Private m_DetailsDocument As String
Public Property DetailsDocument() As String
Get
Return m_DetailsDocument
End Get
Set(value As String)
m_DetailsDocument = value
End Set
End Property
Private m_ClosingDate As DateTime
Public Property ClosingDate() As DateTime
Get
Return m_ClosingDate
End Get
Set(value As DateTime)
m_ClosingDate = value
End Set
End Property
Private m_CompanyName As String
Public Property CompanyName() As String
Get
Return m_CompanyName
End Get
Set(value As String)
m_CompanyName = value
End Set
End Property
' User for HTML
Public Property HTMLData() As String
Get
Return m_Data
End Get
Set(value As String)
m_Data = value
End Set
End Property
Private m_Data As String
End Class
Public Class JobBoard
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property WSID() As String
Get
Return m_WSID
End Get
Set(value As String)
m_WSID = value
End Set
End Property
Private m_WSID As String
Public Property JobDetails() As List(Of JobData)
Get
Return m_JobDetails
End Get
Set(value As List(Of JobData))
m_JobDetails = value
End Set
End Property
Private m_JobDetails As List(Of JobData)
End Class
Public Shared Function getJobsForWSID(ByVal wsid As String, ByRef mj As JobBoard) As JobBoard
Dim mysql As String = RWP.OtherFormats.masterSelect(wsid).ToString.Trim
' HttpContext.Current.Response.Write(mysql) : HttpContext.Current.Response.End()
Dim Conn As New MySqlConnection(dbconn)
Dim da As New MySqlDataAdapter(mysql, Conn)
Dim dt As New DataTable
Conn.Open()
da.Fill(dt)
Dim mytask As DataRow() = dt.[Select]
If dt.Rows.Count > 0 Then
For Each dr As DataRow In mytask
' THIS LINE CAUSES THE ERROR
mj.JobDetails.Add(New JobData() With {.ID = dr("id"), .MAID = dr("master_agency_id"), .DateAdded = dr("date_added"), .ClientID = dr("client_id"), .JobRef = ts(dr("job_ref")), .JobTitle = ts(dr("job_title")), .Location = ts(dr("location")), .JobType = ts(dr("job_type")), .Salary = ts(dr("salary")), .SalaryTerm = ts(dr("salary_term")), .BriefDescription = ts(dr("brief_desc")), .MoreDetails = ts(dr("more_details")), .DetailsDocument = ts(dr("details_document")), .ClosingDate = dr("closing_date"), .CompanyName = dr("company_name")})
Next
End If
da = Nothing
Conn.Close()
Conn.Dispose()
Conn = Nothing
dt = Nothing
Return mj
End Function
This is the compiler output:-
D:\inetpub\vhosts\api.rankcareers.co.uk\httpdocs\App_Code\acesapiws1.vb(254) : error BC30068: Expression is a value and therefore cannot be the target of an assignment.
mj.JobDetails.Add(New JobData() With {.ID = dr("id"), .MAID = dr("master_agency_id"), .DateAdded = dr("date_added"), .ClientID = dr("client_id"), .JobRef = ts(dr("job_ref")), .JobTitle = ts(dr("job_title")), .Location = ts(dr("location")), .JobType = ts(dr("job_type")), .Salary = ts(dr("salary")), .SalaryTerm = ts(dr("salary_term")), .BriefDescription = ts(dr("brief_desc")), .MoreDetails = ts(dr("more_details")), .DetailsDocument = ts(dr("details_document")), .ClosingDate = dr("closing_date"), .CompanyName = dr("company_name")})
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D:\inetpub\vhosts\api.rankcareers.co.uk\httpdocs\App_Code\acesapiws1.vb(254) : error BC32017: Comma, ')', or a valid expression continuation expected.
mj.JobDetails.Add(New JobData() With {.ID = dr("id"), .MAID = dr("master_agency_id"), .DateAdded = dr("date_added"), .ClientID = dr("client_id"), .JobRef = ts(dr("job_ref")), .JobTitle = ts(dr("job_title")), .Location = ts(dr("location")), .JobType = ts(dr("job_type")), .Salary = ts(dr("salary")), .SalaryTerm = ts(dr("salary_term")), .BriefDescription = ts(dr("brief_desc")), .MoreDetails = ts(dr("more_details")), .DetailsDocument = ts(dr("details_document")), .ClosingDate = dr("closing_date"), .CompanyName = dr("company_name")})
Can anyone suggest why this would be?

The specified string is not in the form required for an e-mail address

i have a problem in this code
Imports Microsoft.VisualBasic
Imports System.Net.Mail
Public Class SendEmail
Private _Mailto As String = ""
Public Property Mailto() As String
Get
Return _Mailto
End Get
Set(ByVal value As String)
_Mailto = value
End Set
End Property
Private _MailSub As String = ""
Public Property MailSub As String
Get
Return _MailSub
End Get
Set(ByVal value As String)
_MailSub = value
End Set
End Property
Private _MailBody As String = ""
Public Property MailBody As String
Get
Return _MailBody
End Get
Set(ByVal value As String)
_MailBody = value
End Set
End Property
Private _Msg As String = ""
Public ReadOnly Property Msg As String
Get
Return _Msg
End Get
End Property
Public Sub email()
Try
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient("smtp.gmail.com")
mail.From = New MailAddress("email#gmail.com")
mail.[To].Add(_Mailto)
mail.Subject = _MailSub
mail.Body = _MailBody
' mail.Headers.Add("In-Reply-To", 1)
SmtpServer.Port = 587
SmtpServer.Credentials = New System.Net.NetworkCredential("email#gamil.com", "password")
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
_Msg = " Check Your Mail "
Catch ex As Exception
_Msg = ex.Message
End Try
End Sub
End Class
and the exception is :The specified string is not in the form required for an e-mail address.: any soultion??
It looks like you're initializing _MailTo to an empty string (""), then setting the "To" email address to that value. Unless you're setting the value of that field to some valid email address, the email sender won't recognize an empty string as a valid email address.
(If you are setting the MailTo property from outside the given code, you'll need to provide code to show where you're setting it and what the value is.)

vb.net send mail is failing

What am I missing in the code below? I am getting a failure sending email message.
Private Sub sendTestEmail()
Dim EmailBody As String
EmailBody = "This is a test *****************"
Dim TestEmail As New System.Net.Mail.MailMessage("webserver#companyname.com", "Smilinglily#companyname.com", "TestEmail", EmailBody)
Dim EmailServer As New System.Net.Mail.SmtpClient("localhost")
EmailServer.SendAsync(TestEmail, Me)
End Sub
Looks like your code came from here
It is probably that your SMTP server is not installed or configured on localhost.
It could also be a security problem stopping your localhost SMTP server from forwarding the mail.
I Have written the class which can perform this task easyly.
Imports System.Net.Mail
Public Class GGSMTP_GMAIL
Dim Temp_GmailAccount As String
Dim Temp_GmailPassword As String
Dim Temp_SMTPSERVER As String
Dim Temp_ServerPort As Int32
Dim Temp_ErrorText As String = ""
Dim Temp_EnableSSl As Boolean = True
Public ReadOnly Property ErrorText() As String
Get
Return Temp_ErrorText
End Get
End Property
Public Property EnableSSL() As Boolean
Get
Return Temp_EnableSSl
End Get
Set(ByVal value As Boolean)
Temp_EnableSSl = value
End Set
End Property
Public Property GmailAccount() As String
Get
Return Temp_GmailAccount
End Get
Set(ByVal value As String)
Temp_GmailAccount = value
End Set
End Property
Public Property GmailPassword() As String
Get
Return Temp_GmailPassword
End Get
Set(ByVal value As String)
Temp_GmailPassword = value
End Set
End Property
Public Property SMTPSERVER() As String
Get
Return Temp_SMTPSERVER
End Get
Set(ByVal value As String)
Temp_SMTPSERVER = value
End Set
End Property
Public Property ServerPort() As Int32
Get
Return Temp_ServerPort
End Get
Set(ByVal value As Int32)
Temp_ServerPort = value
End Set
End Property
Public Sub New(ByVal GmailAccount As String, ByVal GmailPassword As String, Optional ByVal SMTPSERVER As String = "smtp.gmail.com", Optional ByVal ServerPort As Int32 = 587, Optional ByVal EnableSSl As Boolean = True)
Temp_GmailAccount = GmailAccount
Temp_GmailPassword = GmailPassword
Temp_SMTPSERVER = SMTPSERVER
Temp_ServerPort = ServerPort
Temp_EnableSSl = EnableSSl
End Sub
Public Function SendMail(ByVal ToAddressies As String(), ByVal Subject As String, ByVal BodyText As String, Optional ByVal AttachedFiles As String() = Nothing) As Boolean
Temp_ErrorText = ""
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient(Temp_SMTPSERVER)
Mail.Subject = Subject
Mail.From = New MailAddress(Temp_GmailAccount)
SMTP.Credentials = New System.Net.NetworkCredential(Temp_GmailAccount, Temp_GmailPassword) '<-- Password Here
Mail.To.Clear()
For i As Int16 = 0 To ToAddressies.Length - 1
Mail.To.Add(ToAddressies(i))
Next i
Mail.Body = BodyText
Mail.Attachments.Clear()
If AttachedFiles IsNot Nothing Then
For i As Int16 = 0 To AttachedFiles.Length - 1
Mail.Attachments.Add(New Attachment(AttachedFiles(i)))
Next
End If
SMTP.EnableSsl = Temp_EnableSSl
SMTP.Port = Temp_ServerPort
Try
SMTP.Send(Mail)
Return True
Catch ex As Exception
Me.Temp_ErrorText = ex.Message.ToString
Return False
End Try
End Function
End Class
Its the way, how to use class:
Dim GGmail As New GGSMTP_GMAIL("MyFromAddress1#gmail.com", "AccPassword", )
Dim ToAddressies As String() = {"ToAddress1#gmail.com", "ToAddress2#gmail.com"}
Dim attachs() As String = {"d:\temp_Excell226.xlsx", "d:\temp_Excell224.xlsx", "d:\temp_Excell225.xlsx"}
Dim subject As String = "My TestSubject"
Dim body As String = "My text goes here ...."
Dim result As Boolean = GGmail.SendMail(ToAddressies, subject, body, attachs)
If result Then
MsgBox("mails sended successfully", MsgBoxStyle.Information)
Else
MsgBox(GGmail.ErrorText, MsgBoxStyle.Critical)
End If
Hope this helps. Good coding

How can i convert the C# code listed below into VB

i am using the Json.net to serialize an object. the specific object is eventInstance.Properties which is the properties of a windows event log.
i am getting a
Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property
for C# an example is shown here
string json = JsonConvert.SerializeObject(joe, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
my line of code is below but i am not sure how to make it work in VB or if it is even possible
command.Parameters.AddWithValue("#f18", JsonConvert.SerializeObject(eventInstance.Properties(), New JsonSerializerSettings() {ReferenceLoopHandling = ReferenceLoopHandling.Ignore}))
i get an error that states 'ReferenceLoopHandling' is an enum type and cannot be used as an expression
thanks for the help
You can use below code:
Private Function getJSON(sJSON As String) As String
Dim objNews = New List(Of News)()
Dim news = New News()
news.id = ""
news.title = "blah"
Dim lst = New List(Of Object)()
lst.Add(New With {.video_identifier = "id1"})
lst.Add(New With {.video_identifier = "id2"})
news.video_identifier = lst.ToArray()
objNews.Add(news)
Return Newtonsoft.Json.JsonConvert.SerializeObject(New With {.data = objNews})
End Function
Class News
Public Property title As String
Get
Return _title
End Get
Set
_title = value
End Set
End Property
Private _title As String
Private _sId As String
Public Property id As String
Get
Return _sId
End Get
Set
_sId = value
End Set
End Property
Private _youtube_videos As Object() = New List(Of Object)().ToArray()
Public Property video_identifier As Object()
Get
Return _youtube_videos
End Get
Set
_youtube_videos = value
End Set
End Property
End Class
Public Class YoutubeVideos
Private _video_identifier As String
Public Property video_identifier As String
Get
Return _video_identifier
End Get
Set
_video_identifier = value
End Set
End Property
End Class

Resources