?
Solved

ASP.Net 2.o Set ObjectDataSource SelectParameters programatically

Posted on 2008-10-09
3
Medium Priority
?
2,220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

743 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