Select data with SqlDataSource in code behind

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

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
 
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
 
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
 
Arjen said:
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.CloseConnection);

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

MyConnection.Close();
 
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 said:
Arjen said:
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.CloseConnection);

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

MyConnection.Close();
 
The ASP.NET QUICKSTARTS is the place to look first for all of this kind of
stuff.
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/sqldatasource.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 said:
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 said:
Arjen said:
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.CloseConnection);

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

MyConnection.Close();
 
Hi,

Thanks!

Finally it works... :)

Arjen



Mr. Arnold said:
Arjen said:
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.CloseConnection);

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

MyConnection.Close();
 
Arjen said:
Hi,

Thanks!

Finally it works... :)

Arjen

You are welcomed. :)
Mr. Arnold said:
Arjen said:
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.CloseConnection);

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

MyConnection.Close();
 
Back
Top