Solved

ASP.Net 2.o Set ObjectDataSource SelectParameters programatically

Posted on 2008-10-09
3
2,198 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now