473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Select data with SqlDataSource in code behind

Hi,

This are the lines I have now:
SqlDataSource1.SelectCommand = "SELECT TOP (1) RangeId FROM myTable";
SqlDataSource1.SelectParameters.Add(new Parameter("RangeId",
TypeCode.Int32));

int rangeId = ...

How do I get the result from the database in the variable? I tried a lot of
samples on the internet but could not find the solution.

Thanks!
Arjen
Jun 4 '07 #1
7 39232
Arjen,
If you don't intend to use the SQLDataSource resultset to bind to some
control such as a gridview, you would probably be better off using a
SqlCommand object directly with the ExecuteScalar method, which is much more
efficient for single values.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Arjen" wrote:
Hi,

This are the lines I have now:
SqlDataSource1.SelectCommand = "SELECT TOP (1) RangeId FROM myTable";
SqlDataSource1.SelectParameters.Add(new Parameter("RangeId",
TypeCode.Int32));

int rangeId = ...

How do I get the result from the database in the variable? I tried a lot of
samples on the internet but could not find the solution.

Thanks!
Arjen
Jun 4 '07 #2
Hi Peter,

I'm only using this for a simple application, one that I only will use this
month.

With the datasource object I also want to update the table. Therefor I want
to keep the code style consistent.

Do you have an example how to use the select and update method with the
datasource object?

Thanks,
Arjen

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comschreef in
bericht news:75**********************************@microsof t.com...
Arjen,
If you don't intend to use the SQLDataSource resultset to bind to some
control such as a gridview, you would probably be better off using a
SqlCommand object directly with the ExecuteScalar method, which is much
more
efficient for single values.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Arjen" wrote:
>Hi,

This are the lines I have now:
SqlDataSource1.SelectCommand = "SELECT TOP (1) RangeId FROM myTable";
SqlDataSource1.SelectParameters.Add(new Parameter("RangeId",
TypeCode.Int32));

int rangeId = ...

How do I get the result from the database in the variable? I tried a lot
of
samples on the internet but could not find the solution.

Thanks!
Arjen

Jun 4 '07 #3

"Arjen" <bo*****@hotmail.comwrote in message
news:f4**********@news2.zwoll1.ov.home.nl...
Hi,

This are the lines I have now:
SqlDataSource1.SelectCommand = "SELECT TOP (1) RangeId FROM myTable";
SqlDataSource1.SelectParameters.Add(new Parameter("RangeId",
TypeCode.Int32));

int rangeId = ...

How do I get the result from the database in the variable? I tried a lot
of samples on the internet but could not find the solution.

Why don't you use a DataReader?

MyDataReader[1] // equals field two of the fields being pulled back in the
recordset record.

Or you can use MyDataReader['caseid'] // if field 0 is the record's key.

You might have to go through a Convert to set a database field to the proper
variable type.

So in your case, the Reader is only to read one record and hit EOF.
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local);
Initial Catalog = CaseManager; Integrated Security=true");

MyConnection.Open();

SqlCommand MyCommand = new SqlCommand("SELECT * FROM CaseInfo",
MyConnection);
SqlDataReader MyDataReader =
MyCommand.ExecuteReader(CommandBehavior.CloseConne ction);

while (MyDataReader.Read())
{
Console.WriteLine(MyDataReader[0] + " " + MyDataReader[1]);
}

MyConnection.Close();
Jun 4 '07 #4
Hi Arnold,

Thanks for your response.

I want to use the SqlDataSource. I know my parameter, my command, but don't
know how to get the row.
SqlDataSource1.Select???

Can you tell me that?

Arjen

"Mr. Arnold" <MR. Ar****@Arnold.comschreef in bericht
news:eg****************@TK2MSFTNGP06.phx.gbl...
>
"Arjen" <bo*****@hotmail.comwrote in message
news:f4**********@news2.zwoll1.ov.home.nl...
>Hi,

This are the lines I have now:
SqlDataSource1.SelectCommand = "SELECT TOP (1) RangeId FROM myTable";
SqlDataSource1.SelectParameters.Add(new Parameter("RangeId",
TypeCode.Int32));

int rangeId = ...

How do I get the result from the database in the variable? I tried a lot
of samples on the internet but could not find the solution.


Why don't you use a DataReader?

MyDataReader[1] // equals field two of the fields being pulled back in
the recordset record.

Or you can use MyDataReader['caseid'] // if field 0 is the record's key.

You might have to go through a Convert to set a database field to the
proper variable type.

So in your case, the Reader is only to read one record and hit EOF.
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local);
Initial Catalog = CaseManager; Integrated Security=true");

MyConnection.Open();

SqlCommand MyCommand = new SqlCommand("SELECT * FROM CaseInfo",
MyConnection);
SqlDataReader MyDataReader =
MyCommand.ExecuteReader(CommandBehavior.CloseConne ction);

while (MyDataReader.Read())
{
Console.WriteLine(MyDataReader[0] + " " + MyDataReader[1]);
}

MyConnection.Close();


Jun 4 '07 #5
The ASP.NET QUICKSTARTS is the place to look first for all of this kind of
stuff.
http://quickstarts.asp.net/QuickStar...atasource.aspx
It also installs with the products if you select it to.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Arjen" wrote:
Hi Arnold,

Thanks for your response.

I want to use the SqlDataSource. I know my parameter, my command, but don't
know how to get the row.
SqlDataSource1.Select???

Can you tell me that?

Arjen

"Mr. Arnold" <MR. Ar****@Arnold.comschreef in bericht
news:eg****************@TK2MSFTNGP06.phx.gbl...

"Arjen" <bo*****@hotmail.comwrote in message
news:f4**********@news2.zwoll1.ov.home.nl...
Hi,

This are the lines I have now:
SqlDataSource1.SelectCommand = "SELECT TOP (1) RangeId FROM myTable";
SqlDataSource1.SelectParameters.Add(new Parameter("RangeId",
TypeCode.Int32));

int rangeId = ...

How do I get the result from the database in the variable? I tried a lot
of samples on the internet but could not find the solution.

Why don't you use a DataReader?

MyDataReader[1] // equals field two of the fields being pulled back in
the recordset record.

Or you can use MyDataReader['caseid'] // if field 0 is the record's key.

You might have to go through a Convert to set a database field to the
proper variable type.

So in your case, the Reader is only to read one record and hit EOF.
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local);
Initial Catalog = CaseManager; Integrated Security=true");

MyConnection.Open();

SqlCommand MyCommand = new SqlCommand("SELECT * FROM CaseInfo",
MyConnection);
SqlDataReader MyDataReader =
MyCommand.ExecuteReader(CommandBehavior.CloseConne ction);

while (MyDataReader.Read())
{
Console.WriteLine(MyDataReader[0] + " " + MyDataReader[1]);
}

MyConnection.Close();


Jun 4 '07 #6
Hi,

Thanks!

Finally it works... :)

Arjen

"Mr. Arnold" <MR. Ar****@Arnold.comschreef in bericht
news:eg****************@TK2MSFTNGP06.phx.gbl...
>
"Arjen" <bo*****@hotmail.comwrote in message
news:f4**********@news2.zwoll1.ov.home.nl...
>Hi,

This are the lines I have now:
SqlDataSource1.SelectCommand = "SELECT TOP (1) RangeId FROM myTable";
SqlDataSource1.SelectParameters.Add(new Parameter("RangeId",
TypeCode.Int32));

int rangeId = ...

How do I get the result from the database in the variable? I tried a lot
of samples on the internet but could not find the solution.


Why don't you use a DataReader?

MyDataReader[1] // equals field two of the fields being pulled back in
the recordset record.

Or you can use MyDataReader['caseid'] // if field 0 is the record's key.

You might have to go through a Convert to set a database field to the
proper variable type.

So in your case, the Reader is only to read one record and hit EOF.
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local);
Initial Catalog = CaseManager; Integrated Security=true");

MyConnection.Open();

SqlCommand MyCommand = new SqlCommand("SELECT * FROM CaseInfo",
MyConnection);
SqlDataReader MyDataReader =
MyCommand.ExecuteReader(CommandBehavior.CloseConne ction);

while (MyDataReader.Read())
{
Console.WriteLine(MyDataReader[0] + " " + MyDataReader[1]);
}

MyConnection.Close();


Jun 4 '07 #7

"Arjen" <bo*****@hotmail.comwrote in message
news:f4**********@news5.zwoll1.ov.home.nl...
Hi,

Thanks!

Finally it works... :)

Arjen
You are welcomed. :)
"Mr. Arnold" <MR. Ar****@Arnold.comschreef in bericht
news:eg****************@TK2MSFTNGP06.phx.gbl...
>>
"Arjen" <bo*****@hotmail.comwrote in message
news:f4**********@news2.zwoll1.ov.home.nl...
>>Hi,

This are the lines I have now:
SqlDataSource1.SelectCommand = "SELECT TOP (1) RangeId FROM myTable";
SqlDataSource1.SelectParameters.Add(new Parameter("RangeId",
TypeCode.Int32));

int rangeId = ...

How do I get the result from the database in the variable? I tried a lot
of samples on the internet but could not find the solution.


Why don't you use a DataReader?

MyDataReader[1] // equals field two of the fields being pulled back in
the recordset record.

Or you can use MyDataReader['caseid'] // if field 0 is the record's key.

You might have to go through a Convert to set a database field to the
proper variable type.

So in your case, the Reader is only to read one record and hit EOF.
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local);
Initial Catalog = CaseManager; Integrated Security=true");

MyConnection.Open();

SqlCommand MyCommand = new SqlCommand("SELECT * FROM
CaseInfo", MyConnection);
SqlDataReader MyDataReader =
MyCommand.ExecuteReader(CommandBehavior.CloseConn ection);

while (MyDataReader.Read())
{
Console.WriteLine(MyDataReader[0] + " " +
MyDataReader[1]);
}

MyConnection.Close();


Jun 4 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Swetha | last post by:
Hello I have a DropDownList that I am populating using the following SqlDataSource: <asp:DropDownList ID="parentIDDropDownList" runat="server" DataSourceID="SqlDataSource3"...
1
by: staeri | last post by:
Hello! I would like to set the SqlDataSource in the code behind file and not to have it in the aspx file. How can I do that? Regards, Staeri
3
by: Dorte | last post by:
Hi, Could someone help me with a couple of links to SqlDatasource documentation on how to use the Gridview and SqlDatasource components in code behind? Basically I'm missing some documentation...
2
by: phil | last post by:
Hi, The connection and DeleteCommand of a gridview are defined in the aspx file like this: <asp:SqlDataSource ID="SqlDataSource1" runat="server"...
1
by: thomson | last post by:
Hi All, i do have an application that searches the databse, For eg, i do, SqlHelper.ExecuteReader("","") this is the point where i call the stored procedure to fetch the data from the database,...
0
by: jmacduff | last post by:
Big question: How to enable edit/update commands to work when setting the sqldatasource select command from code behind. Details: I have a GridView using a sqldatasouce with the select and...
0
by: ADAC | last post by:
I am very familiar with Visual studio 2003 and trying to get a handle on 2005 I can set up the datasource and make it work fine when I databind it to a control like a gridview or details view....
2
by: DotNetNubie66 | last post by:
I have a Master page that wraps my content pages, on one page I have a FormView that is tied to an ObjectDataSource and has textbox and calendar controls bound to the datasource. My problem is this,...
3
by: markpringle | last post by:
This is my sqldatasource this is on the page: <asp:sqldatasource runat="server" ID="DS_CheckUserName" ConnectionString="<%$ ConnectionStrings:SQL2005_355184_ConnectionString %>" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.