Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataSource & Null values

Status
Not open for further replies.

JaneB19

Technical User
Jun 27, 2002
110
GB
Hi,
I'm trying to fill a datagrid from a database depending on what the user has selected on the form.

I have two recurring errors:
1st: Property access must assign to the property or use its value the line of coding highlighted is AdvancedSolutions1.DataSource() where AdvancedSolutions1 is the datagrid ID.

2nd: I have tried setting the AdvancedSolutions1.DataSource() to the objDataReader but I get the following error

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.


With the stack trace as:
[NullReferenceException: Object reference not set to an instance of an object.]
CrypticCrosswordSolver.NewAdvancedSearch1.btnSubmit_Click(Object sender, EventArgs e) in c:\inetpub\ System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


Can anybody help me with either of these errors? If any more coding is needed please let me know and I'll put it up.

Thanks in anticipation

Jane :)
 
Jane,
1 - Do not add a parenthesis next to the DataSource property, because it is a property not a method.
2 - To use the DataSource property, you must either assign it a value, or get the value from it and store it in your own variable.

Try this:
Code:
AdvancedSolutions1.DataSource = objDataReader;
AdvancedSolutions1.DataBind();
Make sure that the objDataReader has been assigned a value before you assign it to the DataSource of AdvanceSolutions1. DataBind() is a method and it does need the parenthesis. You must call it to do the actual binding of AdvancedSolutions1 to objDataReader.

Note: If your using Visual Basic .NET, simply remove the semicolons from my code because they reflect C# code.

Hope this helps...

JC

We don't see things as they are; we see them as we are. - Anais Nin
 
Hi,
Thanks for your reply.

I had tried the coding you suggested before and got the same error message:

Object reference not set to an instance of an object.

Do you by any chance have any other ideas as to why I'd be getting this error.

I don't know if this helps but my coding for this sub is as follows:

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Dim strConnection As New System.Data.OleDb.OleDbConnection("Provider = Microsoft.Jet.OleDb.4.0;Data Source = C:\Inetpub\ Dim myCommand As String
Dim No_Letts As String
Dim ClueBX As String

No_Letts = No_Letters.SelectedItem.Value
ClueBX = Clue_box.Text

strConnection.Open()

If ClueTypeList.SelectedItem.Value = "Abbreviation" Then
myCommand = "SELECT [Abbreviations].[Abb1], [Abbreviations].[Abb2], [Abbreviations].[Abb3], [Abbreviations].[Abb4], [Abbreviations].[Abb5], [Abbreviations].[Abb6], [Abbreviations].[Abb7] FROM [Abbreviations] WHERE ([Abbreviations].[Word]= ' " & ClueBX & " ') AND ((' " & No_Letts & " ' = [Abbreviations].[NoLett1]) OR (' " & No_Letts & " ' = [Abbreviations].[NoLett2]) OR (' " & No_Letts & " ' = [Abbreviations].[NoLett3]) OR (' " & No_Letts & " ' = [Abbreviations].[NoLett4]) OR ( ' " & No_Letts & " ' = [Abbreviations].[NoLett5]) OR ( ' " & No_Letts & " ' = [Abbreviations].[NoLett6]) OR (' " & No_Letts & " ' = [Abbreviations].[NoLett7]));"

End If

If ClueTypeList.SelectedItem.Value = "Word_Exchange" Then
myCommand = "SELECT [Exchange].[Exch1], [Exchange].[Exch2], [Exchange].[Exch3], [Exchange].[Exch4] FROM [Exchange] WHERE (([Exchange].[Clue] = ' " & ClueBX & " ') AND ((' " & No_Letts & " ' = [Exchange].[NoLett1]) OR (' " & No_Letts & " ' = [Exchange].[NoLett2]) OR (' " & No_Letts & " ' = [Exchange].[NoLett3]) OR (' " & No_Letts & " ' = [Exchange].[NoLett4]));"
End If

If ClueTypeList.SelectedItem.Value = "Straight" Then
myCommand = "SELECT [Solution] FROM StraightSolutions WHERE ((' " & No_Letts & " ' = [NoLetters]) AND (SELECT Clue FROM Straight WHERE ([Clue] = ' " & ClueBX & " '))); "
End If

If ClueTypeList.SelectedItem.Value = "Number_Exchange" Then
myCommand = "SELECT [Number] FROM Numbers;"
End If

Response.Write("Clue type = " & ClueTypeList.SelectedItem.Value)
Response.Write("textarea = " & Clue_box.Text)

'Create a command object
Dim objCommand As OleDb.OleDbCommand
objCommand = New OleDb.OleDbCommand(myCommand, strConnection)

'Get a datareader
Dim objDataReader As OleDb.OleDbDataReader
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

'Do the DataBinding
AdvancedSolutions1.DataSource = objDataReader
AdvancedSolutions1.DataBind()

'Close the datareader/db connection
objDataReader.Close()
strConnection.Close()

End Sub


Thanks again for your help

Jane :)
 
On a quick look, your code seems to be fine (except that you're closing a connection that is already close because the OleDbCommand behavior is CloseConnection but that doesn't cause an error).

The only thing that may be wrong is that the AdvancedSolutions1/b] object may not have been properly initialized. Perhaps you have declared this object in code and have not newed it yet. Tell me, is AdvanceSolutions1 a design-time object, or is it declared in code? If it's declared in code, remember that you must use the New keyword as in:
Code:
[ObjectType] AdvanceSolutions1 = [COLOR=blue]New[/color] [ObjectType]
.
It seems this is the only thing that may be wrong with your code. If this is not your case, I'll take a closer look at the code (as I'm in a little hurry now) and see if I find something else that may be wrong.

Hope this helps!

JC

We don't see things as they are; we see them as we are. - Anais Nin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top