locked
How to pass a textbox to an object datasource RRS feed

  • Question

  • User2094278351 posted

    I'm trying to pass a text box value to an object datasource to alter parameters in a sql statement

     

    aspx file:

            
    Symbol</span>&nbsp;<asp:TextBox ID="Symbol" runat="server"></asp:TextBox>
    
    
    
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="WebApplication2.Volume" SelectMethod="GetVolume">
    
                <selectParameters>
                
                        <asp:ControlParameter Name="Tsymbol" ControlID="Symbol" />
            
                </selectParameters>
    
            </asp:ObjectDataSource>

    vb code file

    Imports System
    Imports System.Collections
    Imports System.Configuration
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    Public Class Volume
    
        Private Shared connectionString As String = ConfigurationManager.ConnectionStrings("ChartConnectionString").ToString()
    
        Public Shared Function GetVolume() As DataTable
    
            Dim connection As New SqlConnection(connectionString)
    
            Dim command As New SqlCommand("SELECT [Volume], [HighPrice] FROM [Historical_EOD_Data] WHERE Ticker=(@Tsymbol)) AND [Volume] >0", connection)
    
            Command.Parameters.Add(New SqlParameter("@Tsymbol", SqlDbType.Int))
    
            Dim sdr As SqlDataReader = Nothing
    
            Dim adapter As New SqlDataAdapter(command)
    
            Dim ds As New DataSet("Volumes")
    
            adapter.Fill(ds, "Volumes")
    
            Return ds.Tables("Volumes")
    
        End Function
    
    End Class
    

    Any help would be greatly appreciated

    Saturday, August 31, 2013 7:08 PM

Answers

  • User2103319870 posted

    Hi,

    Please modify your get method like given below

    
    
    Public Shared Function GetVolume(txtStr As Int32) As DataTable
    
            Dim connection As New SqlConnection(connectionString)
    
            Dim command As New SqlCommand("SELECT [Volume], [HighPrice] FROM [Historical_EOD_Data] WHERE Ticker=(@Tsymbol)) AND [Volume] >0", connection)
    
            Command.Parameters.Add(New SqlParameter("@Tsymbol", SqlDbType.Int))
            command.Parameters("@Tsymbol").Value = txtStr
    
            Dim sdr As SqlDataReader = Nothing
    
            Dim adapter As New SqlDataAdapter(command)
    
            Dim ds As New DataSet("Volumes")
    
            adapter.Fill(ds, "Volumes")
    
            Return ds.Tables("Volumes")
    
        End Function

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 2, 2013 12:19 AM

All replies

  • User2103319870 posted

    Hi,

    You need to set the "PropertyName" of objectdatasource which  identifies the public property of the Control from which the ControlParameter class retrieves a value.

    Please modify your object datasource declaration like given below

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="WebApplication2.Volume" SelectMethod="GetVolume">
    
                <selectParameters>
                
                        <asp:ControlParameter Name="Tsymbol" ControlID="Symbol" PropertyName="Text" Type="String" />
            
                </selectParameters>
    
            </asp:ObjectDataSource>

    Please refer the below link for more details

    Control Parameter Class

    Saturday, August 31, 2013 8:53 PM
  • User2094278351 posted

    I get the following error

     

    Server Error in '/' Application.
    --------------------------------------------------------------------------------
    
    
    ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'GetVolume' that has parameters: Tsymbol. 
      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.InvalidOperationException: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'GetVolume' that has parameters: Tsymbol.
    
    Source Error: 
    
    
     An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
    
    Stack Trace: 
    
    
    
    [InvalidOperationException: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'GetVolume' that has parameters: Tsymbol.]
       System.Web.UI.WebControls.ObjectDataSourceView.GetResolvedMethodData(Type type, String methodName, IDictionary allParameters, DataSourceOperation operation) +1506263
       System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1578
       System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
       System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105
       Telerik.Web.UI.RadDataBoundControl.OnPreRender(EventArgs e) +22
       System.Web.UI.Control.PreRenderRecursiveInternal() +83
       System.Web.UI.Control.PreRenderRecursiveInternal() +168
       System.Web.UI.Control.PreRenderRecursiveInternal() +168
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
    
      
    
    
    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044 

    Sunday, September 1, 2013 8:09 AM
  • User2103319870 posted

    Hi,

    Please modify your get method like given below

    
    
    Public Shared Function GetVolume(txtStr As Int32) As DataTable
    
            Dim connection As New SqlConnection(connectionString)
    
            Dim command As New SqlCommand("SELECT [Volume], [HighPrice] FROM [Historical_EOD_Data] WHERE Ticker=(@Tsymbol)) AND [Volume] >0", connection)
    
            Command.Parameters.Add(New SqlParameter("@Tsymbol", SqlDbType.Int))
            command.Parameters("@Tsymbol").Value = txtStr
    
            Dim sdr As SqlDataReader = Nothing
    
            Dim adapter As New SqlDataAdapter(command)
    
            Dim ds As New DataSet("Volumes")
    
            adapter.Fill(ds, "Volumes")
    
            Return ds.Tables("Volumes")
    
        End Function

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 2, 2013 12:19 AM