Solved

ASP.Net 2.o Set ObjectDataSource SelectParameters programatically

Posted on 2008-10-09
3
2,208 Views
Last Modified: 2013-11-07
Hi Guys,

Trying to set objectdatasource select parameters programatically..  I must be missing a step because the following code:



Protected Sub ButtonSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSearch.Click
       
        Dim lastname As String = TextBoxSearch.Text
        ObjectDataSourceGetTPPStudents.TypeName = "StudentsBLL"
        ObjectDataSourceGetTPPStudents.SelectMethod = "GetStudentsByLastName"
        ObjectDataSourceGetTPPStudents.SelectParameters("LastName").DefaultValue = lastname
        ObjectDataSourceGetTPPStudents.SelectParameters.Add("LastName", lastname)

        ObjectDataSourceGetTPPStudents.DataBind()


    End Sub

Generates this error:

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 6:          Dim lastname As String = TextBoxSearch.Text
Line 7:          ObjectDataSourceGetTPPStudents.TypeName = "StudentsBLL"
Line 8:          ObjectDataSourceGetTPPStudents.SelectMethod = "GetStudentsByLastName"
Line 9:          (ERROR HERE) ObjectDataSourceGetTPPStudents.SelectParameters("LastName").DefaultValue = lastname
Line 10:         ObjectDataSourceGetTPPStudents.SelectParameters.Add("LastName", lastname)
Line 11:
 
What to do?
0
Comment
Question by:vstack
  • 2
3 Comments
 
LVL 14

Expert Comment

by:Binuth
ID: 22677138
check  ObjectDataSourceGetTPPStudents is properly initialized , or provide full code
0
 
LVL 2

Accepted Solution

by:
JasonChandler earned 500 total points
ID: 22680198
You are getting this error because you are assigning a value to a parameter that does not exist.
Lets look at your code in detail:

Line 9:          ObjectDataSourceGetTPPStudents.SelectParameters("LastName").DefaultValue = lastname
Line 10:         ObjectDataSourceGetTPPStudents.SelectParameters.Add("LastName", lastname)
you are your assining a value to a parameter that does not exist.
You are Saying that the Value of the Parameter "lastName" should be something then you are adding the Parameter on the next line so the complier will throw an error because you are assigning a value to something that is no existent.

See my code snippet, that should work for you

Hope this helps
See my code snippet for a way fix
        Dim SelectParameter As New Parameter("LastName", Data.DbType.String, lastname)
        ObjectDataSourceGetTPPStudents.SelectParameters.Add(SelectParameter)

Open in new window

0
 
LVL 2

Expert Comment

by:JasonChandler
ID: 22680228
Forgot to add The Methood New Parameter has 3 overloads
(<ParameterName>,<DataType>,<DefaultValue>)

So yours will be
("LastName", Data.DbType.String, lastname)
like in my above example for you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question