|
![]() |
|
| Author |
|
|||||||
|
evil1
Posts: 338
Location: Brisbane, Queensland
|
I'm having troubles passing a dataset between classes. I get the error
System.NullReferenceException I know this is because something isnt initalised, but what. dataSet = BusinessFacade.GetArticles() the part that is giving me trouble is BusinessFacade.GetArticles() I know for a fact that the class it is calling has a populated dataSet as i have printed it out in the class it calls, it just seems to not return it. so Dim dataSet As New ArticlesDS Dim BusinessFacade As New ArticlesBF dataSet = BusinessFacade.GetArticles() calls Public Function GetArticles() As ArticlesDS Dim dataSet As New ArticlesDS Dim dataCategory As New DataAccess.ConnectToServer Try dataCategory.fillDS(dataSet) Catch e As exception Dim exception As exception Throw exception End Try Return dataSet which results in System.NullReferenceException. Can someone tell me what I'm doing wrong (never used VB). |
|||||||
| #0 09:04pm 19/09/05 |
|
|||||||
|
system
|
--
|
|||||||
| #0 |
|
|||||||
|
Raven
Posts: 1117
Location: Melbourne, Victoria
|
Does it not give you the actual line number the error is occuring on? I find it odd that if your naming convention is as per normal (ie, classes = Capitalised, therefore BusinessFacade is a class not an object) you're going to get a NullRef from ::GetArticles()
dataCategory could potentially be Null though, but really more info is needed. |
|||||||
| #1 09:24pm 19/09/05 |
|
|||||||
|
eu4ia
Posts: 728
Location: Brisbane, Queensland
|
I'm guessing, but put:
Dim dataSet As New ArticlesDS Dim dataCategory As New DataAccess.ConnectToServer into the class scope (outside the method). It may be that the object goes out of scope when the method terminates. As I said, I could be wrong - quicker for you to try than for me to write an app to test it. *edit* if that works, then it might be a good idea to have your class implement IDisposable interface and provide a Dispose method where you set the dataset to nothing. but that may be superfluous. I'm rusty as all hell with this stuff. last edited by eu4ia at 21:31:21 19/Sep/05 |
|||||||
| #2 09:31pm 19/09/05 |
|
|||||||
|
hast
Posts: 652
Location: Brisbane, Queensland
|
haha wtf who cares if the object goes out of scope
if someone has strong references to it then it won't be garbage collected. the only way references magically change from object to null is if they are *special*. these references are definitely not special. |
|||||||
| #3 09:33pm 19/09/05 |
|
|||||||
|
evil1
Posts: 339
Location: Brisbane, Queensland
|
Class Admin.aspx.VB
Imports LiquorWorx.DataSchema Imports LiquorWorx.BusinessFacade Public Class Admin Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. End Sub Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dataSet As New ArticlesDS Dim BusinessFacade As New ArticlesBF dataSet = BusinessFacade.GetArticles() End Sub End Class Class ArticlesBF.VB Imports System Imports System.Data Imports LiquorWorx.DataAccess Imports LiquorWorx.DataSchema Public Class ArticlesBF Public Shared Function GetArticles() As ArticlesDS Dim dataSet As New ArticlesDS Dim dataCategory As New DataAccess.ConnectToServer Try dataCategory.fillDS(dataSet) Catch e As exception Dim exception As exception Throw exception End Try Return dataSet End Function End Class Class ConnectToServer.cs using System; using System.Data.SqlClient; using System.Data; using LiquorWorx.DataSchema; namespace LiquorWorx.DataAccess { /// /// Summary description for ConnectToServer. /// public class ConnectToServer { string connectString = "Data Source = localhost; Integrated Security = SSPI; Initial Catalog = LiquorWorx"; string sqlCommandString = "SELECT * FROM Articles"; //Commands for connection SqlConnection liquorWorxConn = new SqlConnection (); SqlCommand selectCMD = new SqlCommand(); SqlDataAdapter liquorDA = new SqlDataAdapter(); public ConnectToServer() { //Initalise default connection strings liquorWorxConn.ConnectionString = connectString; //Initalise default command selectCMD.CommandText = sqlCommandString; selectCMD.Connection = liquorWorxConn; liquorDA.SelectCommand = selectCMD; } public void fillDS(out ArticlesDS articlesDS) { articlesDS = new ArticlesDS(); liquorDA.Fill(articlesDS,"Articles"); /*Test to see data in DataSet Console.WriteLine("Went into fillDS..."); for (int i=0; i < articlesDS.Tables["Articles"].Rows.Count; i++) { System.Console.WriteLine(articlesDS.Tables["Articles"].Rows[i]["Title"].ToString()); System.Console.WriteLine(articlesDS.Tables["Articles"].Rows[i]["Article"].ToString()); System.Console.ReadLine(); } End of test to see data in DataSet*/ } } } last edited by evil1 at 21:49:04 19/Sep/05 |
|||||||
| #4 09:49pm 19/09/05 |
|
|||||||
|
eu4ia
Posts: 729
Location: Brisbane, Queensland
|
if someone has strong references to it then it won't be garbage collectedCorrect. I forgot about that. Just tested it - scope is not the issue. |
|||||||
| #5 09:52pm 19/09/05 |
|
|||||||
|
möoby
Posts: 2928
Location: UK
|
First you need a connection object to connect to your database.
Const strConn As String = "Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=sa;Initial Catalog=mydb;Data Source=xxx" Dim connection As SqlConnection = New SqlConnection(strConn) Assuming your not using a command object, you need either a datareader object or a dataAdapater object. Datareader is fast, forward only cursor that loops throu a stream of rows. You can execute a select statment or a stored procedure that contains a select statment. A dataadapater object serves as a bridge between a dataset and a data source for retrieveing and saving data. The dataadapater class represents a set of database commands and a database connection that you use to fill a dataset and update the data source. Dim command As SqlCommand = New SqlCommand("GetOrders", connection)command.CommandType = CommandType.StoredProcedure Dim adapter As SqlDataAdapter = New SqlDataAdapter(command) Dim ds As New Data.DataSet adapter.Fill(ds) With ds.Tables(0) If .Rows.Count > 0 Then .. and so on. |
|||||||
| #6 01:18am 20/09/05 |
|
|||||||
|
evil1
Posts: 340
Location: Brisbane, Queensland
|
I've done all that in the C# file. Which is then called by a VB class, which is then called by another VB class, it's when the second call happens that their is a problem.
|
|||||||
| #7 07:10am 20/09/05 |
|
|||||||
|
Insom
Posts: 183
Location: Brisbane, Queensland
|
Dim exception as exception what are you, high? |
|||||||
| #8 08:25am 20/09/05 |
|
|||||||
|
eXemplar
Posts: 1237
Location: Brisbane, Queensland
|
Catch e As exception ^ lol |
|||||||
| #9 09:29am 20/09/05 |
|
|||||||
|
Raven
Posts: 1119
Location: Melbourne, Victoria
|
I agree with the comments above.
We actually have a rule here at work that if you've been drinking (ie, for lunch etc) you're not allowed to touch code. :) |
|||||||
| #10 12:17pm 20/09/05 |
|
|||||||
|
parabol
Posts: 1722
Location: Brisbane, Queensland
|
While we are on the topic of VB, can someone please help me with a problem I have. I'd create a new thread but don't like the idea of having 2 VB threads. I mean, 1 thread is more than enough!
OK: A VB client application is trying to use functions that are inside a C/C++ DLL. One particular DLL function expects to receive a c-style string (LPSTR). What should the VB code do to interface with the c/c++ function and be able to successfully send it a string? The guy who wrote the VB code reckons he needs to pass a BSTR by value or something, but not having a clue about VB, I have no idea what he's talking about. He wants me to change my C++ code to 'cater' for his VB program, but I refuse to do so (the Win32 API doesn't either), so I want to find a way for him to change his code and be C compliant. If anyone knows a snippet of VB code that can send a c-style string to an LPSTR-receiving C++ function, I'll be very happy. If you also know how a C function should return a string for use by VB, that would be a bonus :) |
|||||||
| #11 06:29pm 20/09/05 |
|
|||||||
|
dafugg
Posts: 1251
Location: Brisbane, Queensland
|
i think there are two functions to simplify that for him parabol. however, nuts to vb and nuts to .net so I won't be looking in my old msdn stuff for it :)
|
|||||||
| #12 07:24pm 20/09/05 |
|
|||||||
|
eXemplar
Posts: 1242
Location: Brisbane, Queensland
|
IIRC you have to use some sort of array (of string or byte or something) otherwise you can use vb's lame variable pointer either VarPtr(whatever) or StrPrt(whatever), or a mixture of the two. Can't really remember and it's different with every situation.
|
|||||||
| #13 07:31pm 20/09/05 |
|
|||||||
|
parabol
Posts: 1723
Location: Brisbane, Queensland
|
Yep, StrPtr has apparently been removed in VB .NET :/
I've sent the VB coder several links, but he reckons the methods given don't work. I guess if all else fails, I'll 'retire' his VB code and rewrite the frontend myself in a real language. Stupid uni group projects. Stupid VB. /bitterness |
|||||||
| #14 07:41pm 20/09/05 |
|
|||||||
|
Raven
Posts: 1121
Location: Melbourne, Victoria
|
Of course it has, VB.NET uses System::String
|
|||||||
| #15 09:19pm 20/09/05 |
|
|||||||
|
hast
Posts: 653
Location: Brisbane, Queensland
|
parabol if you are using .NET then it is very easy
<DllImport("foo.dll")> _ Public Shared Function Foo (<MarshalAs(UnmanagedType.LPStr)> Byval str as String) End Function if the string is constant or <DllImport("foo.dll")> _ Public Shared Function Foo (<MarshalAs(UnmanagedType.LPStr)> Byval str as StringBuilder) End Function if the string is non-constant EDIT: damm brackets oops i lied.. both need to be marked as LP(T)Str http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondefaultmarshalingforstrings.asp last edited by hast at 22:55:18 20/Sep/05 |
|||||||
| #16 10:55pm 20/09/05 |
|
|||||||
|
system
|
--
|
|||||||
| #16 |
|
|||||||
|
| ||||||||