0

In ObjectDataSource we have a method called SelectMethod and TypeName in which we can specify a method to select data from.

But what is the equivalent method in SqlDataSource to speicify a method for data to select from. If there is no such method, how can I specify a method to select data like in ObjectDataSource

<asp:ObjectDataSource ID="ObjEmployees" runat="server" 
        SelectMethod="GetEmployees" TypeName="AllowPaging.GetData">
</asp:ObjectDataSource>

SqlConnection connection = new SqlConnection("server=NIPUNA-PC\\SQLEXPRESS; database=KTD; Trusted_Connection=yes;");
    string commandText = "SELECT * FROM [Emp]";
    public DataSet GetEmployees()
    {
        SqlCommand cmd = new SqlCommand(commandText, connection);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        return ds;
    }

3 Answers 3

1

See this: https://web.archive.org/web/20211010104134/https://www.4guysfromrolla.com/articles/112206-1.aspx

the SqlDataSource and ObjectDataSource raise their Selecting events immediately before performing the SQL query or invoking the object method to retrieve data. After the data has been retrieved, the Selected events fires. By creating a Selecting event handler, you can examine and massage the parameters used in selecting data;

You can use these event handlers to specify which method is used by the datasource control.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the behind the scene overview and the link. I looking forward to go through the series ASAP....
0

In SqlDataSource, it's equivalent is SelectCommand. You can either provide the Select query and set SelectCommandType to Text(default) or use a stored procedure and set SelectCommandType to StoredProcedure

2 Comments

I know but I want to specify a method as in objectDataSource. (like getEmployee method above) is it possible?
Well, They are two different DataSource providers. Objects have methods; with databases, you have to use a sql query or stored procedure.
0

SQL Data Source executes the SQL inline, so there is no equivalent way of calling a method on an object. You have to use the SeelctCommand to provide the SQL query directly in the UI.

If you are using business components to execute your queries, stick with the ObjectDataSource.

HTH.

1 Comment

Yes after Googling about the difference between SqlDataSourceand the ObjectDataSource, I think I better move on to ObjectDataSource. Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.