Solved

ASP.Net 2.o Set ObjectDataSource SelectParameters programatically

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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