472,811 Members | 3,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 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 39095
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 %>" ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.