Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 12th, 2006, 03:26 PM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Database Queries

I am a coldfusion programmer moving to dotnet, and while I am not having a problem populating asp.net controls with data from my sql server. I can't figure out how to identify a data field value from the database in a conditional statement.
EG

If [table].[field] is X
then [do something]

I am sure it is simple, but I have been working on this for 2 days and can only get information on how to populate controls.

Thanks,

 
Old May 13th, 2006, 03:46 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Ca you show us some code? There's a big difference in how you should compare depending on where you use this code, and what data objects you are using (e.g. in the markup, in code behind with a dataset or a datareader and so on)

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old May 13th, 2006, 11:27 AM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In my example, I will keep the code in the codebehind. When the page loads, I would like to pull one record from the jobDetails table where the jobDetailsID is 1.
Lets say there is one bit field in the jobDetails table called requiresAddress

I would like to set the visibility of a form field based on the value of that bit field.
Here is what I have based on code from a wrox book.

if (!Page.IsPostBack )
        {
            SqlConnection MyConnection;
            SqlCommand MyCommand;
            DataTable MyDataTable;
            SqlDataReader MyReader;
            SqlParameter lobParam;

            MyConnection = new SqlConnection();
            MyConnection.ConnectionString =
        ConfigurationManager.ConnectionStrings["rdConnectionString"].ConnectionString;

            MyCommand = new SqlCommand();
            MyCommand.CommandText =
               " SELECT jobDetailsID, requiresAddress FROM jobDetails WHERE jobDetailsID = @jobDtailsID ";
            MyCommand.CommandType = CommandType.Text;
            MyCommand.Connection = MyConnection;

            lobParam = new SqlParameter();
            lobParam.ParameterName = "@jobDetailsID";
            lobParam.SqlDbType = SqlDbType.Int;
            lobParam.Size = 4;
            lobParam.Direction = ParameterDirection.Input;
            lobParam.Value = "1";

            MyCommand.Parameters.Add(lobParam);

            MyCommand.Connection.Open();
            MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConne ction);



            MyDataTable.Load(MyReader);
 // This is where I don't know what to do

            topinfo.Visible = MyDataTable.requiresAddress;

            MyDataTable.Dispose();
            MyCommand.Dispose();
            MyConnection.Dispose();

        }

 
Old May 13th, 2006, 06:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi frosty,

If all you need to do is check a single field, then you don't need the DataTable. Instead, you can read from the reader directly:

if (MyReader .Read())
{
  someControl.Visible = MyReader .GetBoolean(3);
}

where 3 is the (zero-based) index of the colum you want to read. If you want to improve the readability of the code, you can use GetOrdinal, that returns the index based on a name:

  someControl.Visible = MyReader.GetBoolean(MyReader.GetOrdinal("requiresA ddress"))

And if SomeColumn can be null in the database, you can check it before you try to get a boolean:

if (MyReader.IsDBNull(MyReader.GetOrdinal("requiresAd dress")))
{
  // value is null
}

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Database diagrams (aka Queries) Steve1977 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 September 14th, 2008 07:06 AM
using @cursor in Perl database queries crmpicco Perl 0 March 2nd, 2007 08:33 AM
newbie...database queries.. Deepa7476 Visual Basic 2005 Basics 2 June 16th, 2006 12:41 AM
Combining Queries or results from 2 queries Ford SQL Server 2000 24 November 7th, 2005 08:54 PM
Queries on Back End database? gazolba Access 3 May 1st, 2004 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.