Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.Net 2.o Set ObjectDataSource SelectParameters programatically

Posted on 2008-10-09
3
Medium Priority
?
2,227 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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 …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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