Showing posts with label import. Show all posts
Showing posts with label import. Show all posts

Monday, March 26, 2012

Object reference not set to an instance of an object

HERE'S MY SCRIPT;

<%@dotnet.itags.org. Page Language="VB" Debug="True" CodePage="28599" %>
<%@dotnet.itags.org. Import NameSpace="System.Data" %>
<script runat="server">
Sub Page_Load(Obj As Object,E As EventArgs)
'DataSet Nesnesini Olustur -------
Dim ds as new DataSet("ornek_dataset")
'Tablo Olustur-----------
Dim musteritable as New DataTable("müsteriler")
'Tabloya Sutunlar Ekle--------
musteritable.Columns.Add ("ad",System.Type.GetType("System.String"))
musteritable.Columns.Add ("soyad",System.Type.GetType("System.String"))
musteritable.Columns.Add ("meslek",System.Type.GetType("System.String"))
musteritable.Columns.Add ("yas",System.Type.GetType("System.Int32"))
'Tabloyu DataSete Ekle --------
ds.Tables.Add(musteritable)
'Satir Olusturup, Veri Gir-------
Dim sira As DataRow = musteritable.NewRow
sira("ad")="Zafer"
sira("soyad")="Demirkol"
sira("meslek")="Web Programcýsý"
sira("yas")="33"
musteritable.rows.Add(sira)

Dim sira1 As DataRow = musteritable.NewRow
sira("ad")="Serdar"
sira("soyad")="Benli"
sira("meslek")="Pazarlamacý"
sira("yas")="32"
musteritable.rows.Add(sira1)

Dim sira2 As DataRow = musteritable.NewRow
sira("ad")="Denizhan"
sira("soyad")="Akkaya"
sira("meslek")="Muhasebeci"
sira("yas")="26"
musteritable.rows.Add(sira2)

Dim sira3 As DataRow = musteritable.NewRow
sira("ad")="Levent"
sira("soyad")="Demirkol"
sira("meslek")="Aþcý"
sira("yas")="28"
musteritable.rows.Add(sira3)
'----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-----
Response.Write(ds.Tables("musteriler").rows(2)("ad") & "<br>")

Dim satir_3 As Datarow
satir_3 = ds.Tables("musteriler").rows(2)
Response.Write(satir_3(0) & "<br>")

Dim siralar As DataRowCollection
siralar=ds.Tables("musteriler").rows
Response.Write(siralar(2)("ad") & "<br>")

Dim tablo_degiskenim As DataTable
tablo_degiskenim = ds.Tables("musteriler")
Response.Write(tablo_degiskenim.rows(2)(0) & "<br>")

End sub
</script
AND THE ERROR PAGE;

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 43: musteritable.rows.Add(sira3)
Line 44: '----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-----
Line 45: Response.Write(ds.Tables("musteriler").rows(2)("ad") & "<br>")
Line 46:
Line 47: Dim satir_3 As Datarow

Source File: D:\ASP.NET\dataset_olustur2.aspx Line: 45

I COULDN'T SEE THE MISTAKE, CAN YOU HELP ME?
THANX ...When you created the table, you called it "müsteriler".

Then, when you referenced it on Line 45, you called it "musteriler" ... with no accent ...
Oh yeah ! :)
...

I am bad at my native-lang "Turkish" just like I am bad at English ...
ü is a fuckin' letter in our keyboards ...

...

Thanx NewKid2 ...
If you find this happening too often, considerchanging the language of your computer.

Thursday, March 22, 2012

Object reference not set to an instance of an object.

Not sure what seems to be the problems, been at this code for awhile now and I am fairly new to the whole .NET shift.

<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import NameSpace="System.Data.OleDb" %>
<%
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim myDataReader As OleDbDataReader

myConnection.ConnectionString = "FILE Name=C:\Inetpub\DataConnection\PwrConnDev.UDL"
myConnection.Open()
myCommand = New OleDbCommand( "Select * from tMemberProfilePhoneType", myConnection )
myDataReader = myCommand.ExecuteReader()
While myDataReader.Read()
Response.Write( myDataReader.Item( "Description" ) )
End While
myDataReader.Close()
myConnection.Close()
%>

Any help would be appriciate, also the error is on line: 8myConnection = new OleDbConnection()
You are trying to use your MyConnection object before you instanciate it.

Change:

Dim myConnection As OleDbConnection

to

Dim myConnection As new OleDbConnection

When you do: Dim myConnection As OleDbConnection, all you are doing is creating a pointer that will point to a block of memory, you haven't actually set aside any memory until you call a new statement.
myConnection = new OleDbConnection()

Beat me to it... :D
=) This error is one of those that once you bang your head against it once or twice, you mark it for a slow, silent and painful execution whenever it pops up. You loathe it that much...
myConnection = new OleDbConnection()

Worked like a charm! Thank you! :D
=) This error is one of those that once you bang your head against it once or twice, you mark it for a slow, silent and painful execution whenever it pops up. You loathe it that much...

One of the products of learning to program in a C/C++ world is a very forced (and painful) understanding of pointers...even if they don't like to call them that anymore.
They've done a pretty good job of eliminating the need to understand it in sharp. Actually, Ive never really had to refer to my pointer knowledge and I am doing some very complex stuff these days.

This actually has more to do with initializing a class definition, although the side effect in C++ would have been, admittedtly, a null pointer reference. =)

Friday, March 16, 2012

Object reference not set to an instance of an object.

Hi All

When using the this code


<%@dotnet.itags.org. Page Language="VB" %>
<%@dotnet.itags.org. import Namespace="System.Data" %>
<%@dotnet.itags.org. import Namespace="System.Data.SqlClient" %>
<script runat="server"
Sub Button2_Click(sender As Object, e As EventArgs)

Dim conn As new sqlconnection ("server='sql2.redstation.co.uk'; user id='2128779.150'; password='CLT42876'; database='2128779.150'")

dim dbcommanda as new sqlcommand("SELECT * from tblReg", conn)

dim myDataSet as New Dataset()

dim daprods as new sqldataadapter(dbcommanda)

daprods.fill(myDataSet,"Tony")

if myDataSet.Tables("Details").rows.count = 0 then

response.write("Empty")
end if

conn.close()

I get this error

Server Error in '/' Application.
------------------------

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:

Can Anyone tell me what i am doing wrong?

rgs

TonyPlease specify what line the error occurs on. That will make it lots easier to diagnose.
At first glance, I think the problem might be that you are trying to reference a datatable in the dataset that doesn't exist - Details. When you filled the dataset...

daprods.fill(myDataSet,"Tony")

.. a datatable was created named Tony. The data would then have been loaded into that table. Next, you try to get info about a datatable named Details...

if myDataSet.Tables("Details").rows.count = 0 then

.. which doesn't exist. Two things you can do in order to test this theory:

1. don't provide any datatable name during fill():
daprods.fill(myDataSet).
Then reference the datatable by its ordinal value:
if myDataSet.Tables(0).rows.count = 0 then

2. Try change Tony to Details: daprods.fill(myDataSet,"Details")

Hopefully this helps or at least points you in the right direction.

Let me know if this works :-]
The problem appears to be that in your Fill call, you're telling it to fill a table called Tony. In your check for an empty return, you're checking against a table called Details. There is no Details table in your dataset, so you're trying to reference an object that doesn't exist, which is why you're getting that error.
JBR thanks

ProgUser....thanks for the tips...worked a treat