5 Replies - 4830 Views - Last Post: 08 March 2013 - 09:39 AM Rate Topic: -----

#1 troyb408  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-August 12

Count data table rows

Posted 07 March 2013 - 07:16 AM

Hello all -

I've tried a few different ways to count the rows in my data table. If the count equals 0 i have a custom error message displayed but the issue I am running into is use of unassigned local variable. Here is my code

I'm trying to use dt.count == 0 but says my dt variable is unassigned

Thanks to all that help out

    protected void btnGo_Click(object sender, EventArgs e)
    {
        //dal.Analytic.AdHoc.Snapshot_OccDataTable dt;
        dal.Analytic.AdHoc.adhoc_OccWagesDataTable dt;
        
        
        //retrieves year and stores in currYear variable
        Int16 currYear = (Int16)Cea.Config.ConfigStore.GetConfigInt("Date_OccMatrix");

        //user selects years 1-10 subtract that to get historic year 
        if (Convert.ToInt16(txtYear.Text) > 10)
        {
            Alert("Year can not be more than 10");
            return;
        }
        else if (dt.Count == 0)
        {

            dNoDta.Visible = true;
        }        

        else             
        {
             //stores current year and year2 / yrs constant to pass to query
            Params["year"] = currYear;
            Params["year2"] = currYear - Convert.ToInt16(txtYear.Text);
            Params["yrs"] = Convert.ToInt16(txtYear.Text);
            Params["year3"] = Params["year2"];            
        }




Is This A Good Question/Topic? 0
  • +

Replies To: Count data table rows

#2 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,470
  • Joined: 07-April 08

Re: Count data table rows

Posted 07 March 2013 - 07:34 AM

that is because it isn't assigned. You've declared it, but you never tell dt what it holds.

you need to fill the datatable from something likely a database.
EX:
SqlDataAdapter da = new SQLDataAdapter("SELECT * FROM FOO",con);

da.fill(dt)

if(dt.Rows.Count() == 0) 
{
   //do stuff
}
else
{
  //do other stuff
}


Was This Post Helpful? 1
  • +
  • -

#3 troyb408  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-August 12

Re: Count data table rows

Posted 07 March 2013 - 07:43 AM

Thanks figured i could declare it then use the count method but makes sense needs to hold something before it can count
Was This Post Helpful? 0
  • +
  • -

#4 tycos  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 53
  • Joined: 12-February 13

Re: Count data table rows

Posted 07 March 2013 - 03:58 PM

Before accessing an object you should check it is not Nothing.

This post has been edited by tycos: 07 March 2013 - 03:58 PM

Was This Post Helpful? 1
  • +
  • -

#5 troyb408  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-August 12

Re: Count data table rows

Posted 08 March 2013 - 08:27 AM

After I checked the object to make sure it was filled this is the back end of the sql query:

	SELECT	
		0,
		--@soc_code,
        '00-0000',  
		10000 as sortOrder 			 
		
)mtx   --vSoc table pulls all soc_code and is joined to display soc code and title




)d	
	ON mtx.soc_code = d.soc_code
		WHERE d.mean_his is not null
		--GROUP BY s.soc_code, s.soc_type, s.title, sortOrder  
		ORDER BY sortOrder, soc_code asc	--sorts soc codes if user didnt select to 7 digit level



I've joined a few other tables. What I'm asking for is a second look to see what i'm missing. This sorts the data i want by soc code 00-0002 00-0003 etc. But the last two rows are what is throwing it off they appear as 00-0000 and 00-0001. What I want is the 00-0001 at the top and the 00-0000 at the bottom any suggestions?
Was This Post Helpful? 0
  • +
  • -

#6 troyb408  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-August 12

Re: Count data table rows

Posted 08 March 2013 - 09:39 AM

Nevermind I figured it out. Did a case statement in my initial select statement which separated the two rows from 1 - 99 and then sorted my 00 row and placed at the bottom

CASE WHEN s.soc_code = '00-0000' then 99 else 1 end as sortOrder


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1