469,328 Members | 1,294 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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 38578
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by staeri | last post: by
3 posts views Thread by Dorte | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.