error in send parameter to crystal report

flodi

Member
Joined
Jan 23, 2008
Messages
15
Location
Makkah Al Mukarramah, Makkah, Saudi Arabia, Saudi
Programming Experience
Beginner
hi
i make a program with vb.net2005 and database sql express 2005
i would like to press a print buuton to send a parameter in an inputbox to put value in it
here is my code:
VB.NET:
dim mm1 as string
mm1=inputbox("enter number")
sqlconection1.open
dim cm as sqlclient.sqlcommand
cm=new sqlclient.sqlcommand
cm.connection=sqlconnection1
cm.commandtext="select * from table1 where id_no =' " & trim ( mm1 ) & " ' "
cm.commandtype=commandtype.text
dim read as sqlclient.sqldatareader
read=cm.executereader
if not read.Read then
msgbox("wrong number")
read.close
sqlconnection1.close
else
dim param as new parameterfields
dim pr as sqlparameter
pr=new sqlparameter("@pid_no",sqldbtype.nvarchar)
pr.value=mm1
dim pr1 as parameterfield
pr1.parameterfieldname="@pid_no"
pr1.currentvalue.add(pr)
param.add(pr1)
crystalreportviewer1.parameterfieldinfo=param
crystalreportviewer.visibale=true
sqlconnection1.close
afte i run the inputbox show up
i enter the value and press ok
an error show said:
Object Reference not set to an instance of an Object
in that line:
VB.NET:
pr1.parameterfieldname="@pid_no"
why this error accured?plz help me
and thx
 
If "parameterfield" is a class, which it appears to be, you have to create a new object of this type before using it. You have declared a variable (pr1) of that type, but you have not created a new object, and you have not assigned that object to the variable. Creating an object is done with the New keyword, assigments is done with the = assignment operator. Example:
VB.NET:
Dim x As Button = New Button
The part before the assignment operator is called declaring a variable, it uses the Dim keyword for local declares, next is the name of the variable (x), the As clause is used to specify which type values/objects the variable can hold, and finally "Button" here is the type, a standard .Net class representing a button control incidentally. After the assignment operator you see the New keyword in action, it is followed by the type name for the object that is to be created. Using the New keyword on a class type invokes one of the Sub New methods of that class, these are called "constructors" and exist for the purpose of initializing the class object with default values, a constructor can also be empty if nothing need to be done when the object is created.

Once you understand what all those parts in the above code line means you can get all fancy with shortcuts and stuff and throw it all into apparently one single instruction:
VB.NET:
Dim x As New Button
That code line does all the same three operations as the previous example, but hides the complexisites of object creation and assignment somewhat, the New keyword kind of sneaks into the declaration anonymously.

About the error message, "Object reference not set to an instance of an object", this sounds a bit convoluted, and it is a bit confusing at first. I will explain briefly; variables can hold simple values like the number 1 or a reference to a class object, they are called value types and reference types. A value type variable holds the actual value, while a reference type variable just refers to the object placed somewhere in computer memory. Reference types allows different parts of the program to handle and refer to the same object, where for value types a new copy of the value is created each time it is passed on. In this case you have a reference variable that does not point to any object yet, so "pr1" is your object reference and the problem is that it is not set to point to an object instance (which you also have not created). Let's say you did create a New object, then dereferenced it with the instruction "pr1 = Nothing", then you would also get the same error even if you did create an object, because after assigning the Nothing (null reference) the pr1 object reference once again does not point to an instance of that object type. Perhaps you also noticed the title of the error dialog, it said "NullReferenceException". Visual Studio has more in store for you, if you look at the Error List you will also find a warning for this variable that reads this:
Variable 'pr1' is used before it has been assigned a value. A null reference exception could result at runtime.
 
thx gohnh
i understand that i must type it like that:
dim pr1 as new parameterfield
and it did work fine
but another error show up in line:
pr1.currentvalue.add(pr)
and it said ( invalid object type )
actually i don't know what that line mean!all i know that after i type my number it should pass it with all records to my reports
help me in that johnh
and thx
 
I don't know the CR object library, you'd have to look up what the Add method wants as parameter in the CR documentation, it obviously doesn't like "sqlparameter". Perhaps you can also use the intellisense option list or the Object Browser to see what parameter types is expects, but some libraries won't reveal it and only displays Object as type, in which case you have to rely on what the documentation says. Did you also try searching the web? I'm sure sending parameters to CR is a common issue.
 
How to pass parameters after setting datasource path using connectioninfo to crystal report in vb 2005. I am able to set datasource path but unable to set parameters to report
 
Back
Top