Monday, March 12, 2012

how to convert sqldatasource to other type like dataview, dataset or datareader

Hi all,

i have got many problems with sqldatasource and dataview

--------------------------------
string ConnectionString = "Server=server;Integrated
Security=True;Database=contest;Persist Security Info=True";

string QueryString = "SELECT * FROM [members]";

System.Web.UI.WebControls.SqlDataSource SqlDataSource = new System.Web.UI.WebControls.SqlDataSource();

SqlDataSource.ConnectionString = ConnectionString;
SqlDataSource.SelectCommand = QueryString;
SqlDataSource.DataSourceMode = System.Web.UI.WebControls.SqlDataSourceMode.DataSet;

DataView dv = new DataView();

dv = (DataView)SqlDataSource;
--------------------------------

CS0030: Cannot convert type 'System.Web.UI.WebControls.SqlDataSource' to 'System.Data.DataView'

--------------------------------

i got similar problems if i use DataSet and DataReader.
SqlDataSource.DataSourceMode = System.Web.UI.WebControls.SqlDataSourceMode.DataSet;
SqlDataSource.DataSourceMode = System.Web.UI.WebControls.SqlDataSourceMode.DataReader;

--------------------------------

thx a lot.To return the DataSet from the SqlDataSource you have to use the SqlDataSource Select method. The Select method will return a DataView if the DataSourceMode of the SqlDataSource is set to System.Web.UI.WebControls.SqlDataSourceMode.DataSet.

If you set the mode to System.Web.UI.WebControls.SqlDataSourceMode.DataReader. A DataReader will be returned.
thx,
but, i dont know how to use sqldatasource as dataview after set it to dataset.
u have an example ?
thx
What is your purpose with the code? Do you only want to access a data source and get a Dataset back? If so I should recommend you to use SqlDataAdaptar:


DataSet ds = new DataSet();

using(SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Employees", myConnectionString))
{
adapter.Fill(ds);
}

If you want to use SqlDataSource for some reason, here is an example:


string ConnectionString = "Server=localhost;Integrated Security=True;Database=Northwind;User=****;password=****;";
string QueryString = "SELECT * FROM [Employees]";
System.Web.UI.WebControls.SqlDataSource SqlDataSource = new System.Web.UI.WebControls.SqlDataSource();
SqlDataSource.ConnectionString = ConnectionString;
SqlDataSource.SelectCommand = QueryString;

DataSourceSelectArguments arg = new DataSourceSelectArguments();
System.Data.DataView dv = (System.Data.DataView)SqlDataSource.Select(arg);


okay thanks, now continuing on with system.data.dataview dv = ..., how do you retrieve a value, can you finish the code for us?
Many thanks,
Asaf

Here is the thing

DataView dv ;

dv = (DataView)DsOpenOrders.Select(DataSourceSelectArguments.Empty);

dv.Sort = SortField;

return dv;

DsOpenOrders is the SqlDataSource.

THis will help you I guess.

Thanks
Suda

Labels: , , , , , , , , ,

How to convert SqlDatareader to DataTable

Hello all,

Is there a way to convert a SqlDatareader to DataTable?

I'm not talking about looping through the datareader and add each value into the datatable. For example the dataset, you use a DataAdapter to fill the data into the dataset.

I've read on something like a ovrride the fill function and of the DbDataAdapter ... but it doesn't seem to work ...

protected overridable function(dataTable as DataTable, dataReader as IDataReader) as integer
return Fill(dataTable, dataReader)
end function

Has anyone done this before? Any input is appreciated.

Unfortunately, as far as I know, the answer is no - due to the inherent nature of a DataReader itself. It's a forward only/read once process. If you don't do something with it at the time it's being created, it's gone.

The only way I can think of to take care of that, would be to manually insert the objects into the datatable, as the datareader was being read


David, your icon is scary ... lol. thanks for the respond. please take a peek at this URL:http://msdn2.microsoft.com/en-us/library/497b1k4t.aspx

and also this sample:http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=628

I tried but somehow I can't get it to run. Well, I didn't copy all of the author's code. What do you think?

Labels: , , , , , , , ,

How to convert datareader.items value to integer

Hi,

I have a datareader item

myreader.Item("netcost")

This is a numeric value, I need to take the value and minus 500 from it, I have

(myreader.Item("netcost")-500)

This causes an error, 'cannot cast from string to integer'

How do I do it?

Thanks. BenYou can use Ctype.

For example, this converts you datareader item to an integer.

CType(myreader.Item("netcost"),Integer) - 500
Convert.ToInt32(myreader.Item("netcost").ToString())

Labels: , , , , , , , , , , ,

How to convert a datareader to datatable?

How to convert a datareader to datatable?DataTable.Load?
--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
"abc my vclass" <myabcclass@.myclass.com> wrote in message
news:eu$Ato6AGHA.2544@.TK2MSFTNGP11.phx.gbl...
> How to convert a datareader to datatable?
>
ABC,
In the same way as that you convert an engine to a wheel of a car.
A datareader is a class that is used by the dataadapter to load a datatable.
I hope this gives an idea.
Cor
Check this blog post:
Convert a DataReader into a DataSet
http://weblogs.asp.net/rosherove/archive/2004/01/22/61541.aspx
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"abc my vclass" <myabcclass@.myclass.com> wrote in message
news:eu$Ato6AGHA.2544@.TK2MSFTNGP11.phx.gbl...
> How to convert a datareader to datatable?
>
Teemu,
Why do you think that as written on the blog page that it is a fast
solution. We have almost as long as our website exist this sample on our
website, however I will never tell that it is fast. It is slow, but can be
used if someone wants a progressbar. And than he/she should have a huge
amount of data and accept that it is going a little bit slower.
http://www.vb-tips.com/default.aspx?ID=49f2cff5-56ad-44fc-a4c6-fc0d5c470f53
Just my thougth,
Cor
Cor,
> Why do you think that as written on the blog page that it is a fast
> solution. We have almost as long as our website exist this sample on our
> website, however I will never tell that it is fast.
Did I say something like that? Which one of those few implementations I've
said is fast? There are quite many variations on that blog, if you check
them (comments also) Not sure if I follow your question, though.
> It is slow, but can be used if someone wants a progressbar. And than
> he/she should have a huge amount of data and accept that it is going a
> little bit slower.
> http://www.vb-tips.com/default.aspx?ID=49f2cff5-56ad-44fc-a4c6-fc0d5c470f53
Ah, If you mean which one I'd use, I'd take the one inheriting from
DBDataAdapter or the one using BeginLoadData and LoadDataRow (DBDataAdapter
uses that internally, so the selection goes to scenario preference). Why?
Because using BeginLoadData turns off constraints , index maintaining etc
when loading data.
Regards,
Teemu
Teemu,
> Ah, If you mean which one I'd use, I'd take the one inheriting from
> DBDataAdapter or the one using BeginLoadData and LoadDataRow
> (DBDataAdapter uses that internally, so the selection goes to scenario
> preference). Why? Because using BeginLoadData turns off constraints ,
> index maintaining etc when loading data.
>
I like just the DBDataadapter.
The slight performance that is gained by other solutions, is in my opinion
lost by wors maintainability.
Cor
Like Miha mentions, Datatable.Load works great ifyou're using the 2.0
Framework, but out of curiousity, what is the challenge your wanting to
address by doing this?
"abc my vclass" <myabcclass@.myclass.com> wrote in message
news:eu$Ato6AGHA.2544@.TK2MSFTNGP11.phx.gbl...
> How to convert a datareader to datatable?
>

Labels: , , , ,