![]() |
|
|||||||
| Reporting / Printing Discussion on output-creation components such as reporting, pdf, etc. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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: Code:
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
i enter the value and press ok an error show said: Object Reference not set to an instance of an Object in that line: Code:
pr1.parameterfieldname="@pid_no" 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:
Code:
Dim x As Button = New Button 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: Code:
Dim x As New Button 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: Quote:
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
|
|||
|
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.
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|