Friday, March 16, 2012

Object reference not set to an instance of an object. - Help!

Hi! Somebody plz help me with this problem? I don't know what's happen.

In may web.config i have my connection, then i get this error when compile de code.

</system.web>

<appSettings>

<addkey="Conexao"value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Economato\Economato.mdb;"/>

</appSettings>

</configuration>

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 37: .DataTextField = "Estado"Line 38: .DataValueField = "Estado"Line 39: .DataSource = GetDados(SqlString).Tables("dados").DefaultViewLine 40: .DataBind()Line 41: End With

Source File:C:\Inetpub\wwwroot\Economato\Utilizador\Requisita.aspx.vb Line:39
First off, may I suggest not putting absolute paths in your connection string. Change it to...

<addkey="Conexao"value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Economato.mdb;"/>
Also, we need to see the code on the page that has.DataSource = GetDados(SqlString).Tables("dados").DefaultView
Looks like you may have a blank field in your database (maybe) that you didn't account for when reading.
Or, your SqlString was not set, or your table "dados" is mispelled.
Could be any number of things,
Zath


My SqlString is this:

PrivateSub Page_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

'Put user code to initialize the page here

IfNot Page.IsPostBackThen

Dim SqlStringAsString = "SELECT Distinct Estado FROM ItemRequisicao"

With dropestado

.DataTextField = "Estado"

.DataValueField = "Estado"

.DataSource = GetDados(SqlString).Tables("dados").DefaultView

.DataBind()

EndWith

dropestado_SelectedIndexChanged(sender, e)

EndIf

EndSub


The code on the page that has the data source is this:
(I've 2 function to getdados and get the value that i need)

PublicFunction GetDados(ByVal SqlStringAsString)As DataSet

Try

Dim ConnAsNew OleDbConnection(Conexao)

Dim daAsNew OleDbDataAdapter(SqlString, Conn)

Dim dsAsNew DataSet

Conn.Open()

da.Fill(ds, "dados")

Conn.Close()

Return ds

Catch eAs OleDbException

'Throw e

EndTry

EndFunction

PublicFunction GetValorPedido(ByVal SqlStringAsString)AsString

Try

Dim ConnAsNew OleDbConnection(Conexao)

Dim CmdAsNew OleDbCommand(SqlString, Conn)

Conn.Open()

Dim ValorAsString = Cmd.ExecuteScalar()

Conn.Close()

Return Valor

Catch eAs OleDbException

Throw e

EndTry

EndFunction


I've this code for my SqlString:

PrivateSub Page_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

'Put user code to initialize the page here

IfNot Page.IsPostBackThen

Dim SqlStringAsString = "SELECT Distinct Estado FROM ItemRequisicao"

With dropestado

.DataTextField = "Estado"

.DataValueField = "Estado"

.DataSource = GetDados(SqlString).Tables("dados").DefaultView

.DataBind()

EndWith

dropestado_SelectedIndexChanged(sender, e)

EndIf

EndSub
Then i've this 2 functions:

PublicFunction GetDados(ByVal SqlStringAsString)As DataSet

Try

Dim ConnAsNew OleDbConnection(Conexao)

Dim daAsNew OleDbDataAdapter(SqlString, Conn)

Dim dsAsNew DataSet

Conn.Open()

da.Fill(ds, "dados")

Conn.Close()

Return ds

Catch eAs OleDbException

'Throw e

EndTry

EndFunction

PublicFunction GetValorPedido(ByVal SqlStringAsString)AsString

Try

Dim ConnAsNew OleDbConnection(Conexao)

Dim CmdAsNew OleDbCommand(SqlString, Conn)

Conn.Open()

Dim ValorAsString = Cmd.ExecuteScalar()

Conn.Close()

Return Valor

Catch eAs OleDbException

Throw e

EndTry

EndFunction


Change your code to the following and try again:

With dropestado

.DataSource = GetDados(SqlString).Tables("dados").DefaultView
.DataTextField = "Estado"

.DataValueField = "Estado"

.DataBind()

EndWith


Nothing!...I don't know what's happen, but the problem continues. I already verified the database, but all Ok meets there!

Try this:

.DataSource = GetDados(SqlString)


Ok! Now i've the same problem in other side of my code :s

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 75: Dim SqlString As StringLine 76: SqlString = "SELECT Requisicao.NumReq, Requisicao.DataRequisicao, Requisicao.Codigo_utilizador, ItemRequisicao.Estado FROM Requisicao INNER JOIN ItemRequisicao ON Requisicao.NumReq = ItemRequisicao.NumReq"Line 77: SqlString += "WHERE ItemRequisicao.Estado = '" & dropestado.SelectedItem.Value & "'"Line 78: With dgRequisicaoLine 79: .DataSource = GetDados(SqlString).Tables("dados").DefaultView

Source File:C:\Inetpub\wwwroot\Economato\Utilizador\Requisita.aspx.vb Line:77
The function is this:

PrivateSub dropestado_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles dropestado.SelectedIndexChanged

Dim SqlStringAsString

SqlString = "SELECT Requisicao.NumReq, Requisicao.DataRequisicao, Requisicao.Codigo_utilizador, ItemRequisicao.Estado FROM Requisicao INNER JOIN ItemRequisicao ON Requisicao.NumReq = ItemRequisicao.NumReq"

SqlString += "WHERE ItemRequisicao.Estado = ' " & dropestado.SelectedItem.Value & " ' "

With dgRequisicao

.DataSource = GetDados(SqlString).Tables("dados").DefaultView

.DataBind()

EndWith

With dgmateriais

.DataSource =Nothing

.DataBind()

EndWith

EndSub


Hi,
any chance that there would be no item selected in DropDownList in question (SelectedIndex is -1), when SelectedItem property would return null and trying to access its Value proeprty would then throw NullreferenceException.

Try to debug your code and comment out the green code and see if the DDL is populated or not.

IfNot Page.IsPostBackThen

Dim SqlStringAsString = "SELECT Distinct Estado FROM ItemRequisicao"

With dropestado

.DataSource = GetDados(SqlString)
.DataTextField = "Estado"

.DataValueField = "Estado"

.DataBind()

EndWith

'dropestado_SelectedIndexChanged(sender, e)

EndIf

EndSub


Ok! I do that and the is what u said, the DDL is not populated. But, what i don't understand is why not? Could somebody tell me?

You may try something like these first:

PublicFunction GetDados(ByVal SqlStringAsString)As DataSet

Try

Dim ConnAsNew OleDbConnection(Conexao)

Dim daAsNew OleDbDataAdapter(SqlString, Conn)

Dim dsAsNew DataSet

Conn.Open()

da.Fill(ds, "dados")

Conn.Close()
'See if there is any data back

Dim intRetrun as Integer = ds.Tables("dados").Rows.Count

Return ds

Catch eAs OleDbException

'See if you can catch any exception here
Throw e

EndTry

EndFunction


Hi! Sorry for the time, but i had some problems and just now i've time to work again in my project. I tried that and an exception come and i think that the real problem is with the connection, but i want know if i'm right.

Server Error in '/Economato' Application.

N?o foi possível encontrar o arquivo 'C:\WINDOWS\system32\Economato.mdb'.

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.Data.OleDb.OleDbException: N?o foi possível encontrar o arquivo 'C:\WINDOWS\system32\Economato.mdb'.
Source Error:

Line 57: Catch e As OleDbExceptionLine 58: 'See if you can catch any exception hereLine 59: Throw eLine 60: End TryLine 61: End Function


Could you translate the messages that you posted? That could help identify what's going on.

Thanks for the aid, but I think that already solved the problem for now.Was a problem that had with the connection with the Database.
Thank u!

0 comments:

Post a Comment