473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

data binding with grid view and sqldatasource

69
Hi guys,
I would like some help please. I have a grid view and an sql datasource that returns results of a search from a database using a stored procedure. I did all of this by drag and drop, not manually. In my sqldatasource_selected method, i have a label that suppose to display the number of records returned and it is not doing that, the first time, sometimes, it some how it is moody and out of sync. I tested the code with less than 10 records and it is working. however when i tested it with 253 records it is when it is giving me trouble. Somehow, the first time i press the find button for the first search it is not entering the SQLDataSource3_selected() method, so the label does not get a value. I know this because i steped through the program using break points. I am not sure if it has something to do with the databinding lifecycle and stuff is being executed before its time or something
Any help would greatly be appreciated

here is my code
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class Centre_Interface_Find : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.  
  17.     }
  18.  
  19.     protected void resetButton_Click(object sender, EventArgs e)
  20.     {
  21.         Response.Redirect("Centre_Interface_Find.aspx");
  22.     }
  23.     protected void allCheckBox_CheckedChanged(object sender, EventArgs e)
  24.     {
  25.  
  26.         if (allCheckBox.Checked == true)
  27.         {
  28.             centreListBox.Enabled = false;
  29.             descriptionTextBox.Enabled = false;
  30.             mcDropDown.Enabled=false;
  31.             ddlArea.Enabled = false;
  32.         }
  33.         else
  34.         {
  35.             centreListBox.Enabled = true;
  36.             descriptionTextBox.Enabled = true;
  37.             mcDropDown.Enabled = true;
  38.             ddlArea.Enabled = true;
  39.         }
  40.  
  41.     }
  42.     protected void findButton_Click(object sender, EventArgs e)
  43.     {
  44.         msgLabel.Visible = false;
  45.         msgLabel1.Visible = false;
  46.         if (allCheckBox.Checked == false)
  47.         {
  48.             if (centreListBox.SelectedIndex==0)
  49.                 Session["CentreNo"] = "0";
  50.             else
  51.                 Session["CentreNo"] = centreListBox.SelectedValue;
  52.  
  53.             if (descriptionTextBox.Text.Trim() == "")
  54.                 Session["description"] = "0";
  55.             else
  56.                 Session["description"] = descriptionTextBox.Text.Trim();
  57.  
  58.             if (mcDropDown.SelectedIndex == 1)//Selected Yes
  59.                 Session["mc"] = "1";
  60.             if (mcDropDown.SelectedIndex == 2)//Selected No
  61.                 Session["mc"] = "0";
  62.             if (mcDropDown.SelectedIndex == 0)//selected Both
  63.                 Session["mc"] = "2";
  64.             if (ddlArea.SelectedIndex == 0)
  65.                 Session["area"] = 0;
  66.             else
  67.                 Session["area"] = 1;
  68.             Session["all"] = 0;
  69.  
  70.         }
  71.         else
  72.         {
  73.             Session["CentreNo"] = "0";
  74.             Session["description"] = "0";
  75.             Session["mc"] = "%";
  76.             Session["area"] = 0;
  77.             Session["all"] = 1;
  78.         }
  79.         DataBind();
  80.         resultLabel.Visible = true;
  81.  
  82.  
  83.             if (afGridView.Rows.Count > 0)
  84.             {
  85.                 afGridView.Visible = true;
  86.                 msgLabel.Visible = true;
  87.                 msgLabel1.Visible = true;
  88.             }
  89.  
  90.  
  91.  
  92.     }
  93.     protected void afGridView_DataBound(object sender, EventArgs e)
  94.     {
  95.  
  96.         if (afGridView.PageCount != 0)
  97.         { 
  98.             msgLabel.Text = string.Format("You are viewing page {0} of {1}...", afGridView.PageIndex + 1, afGridView.PageCount);
  99.         }
  100.  
  101.     }
  102.     protected void SqlDataSource3_Selected(object sender, SqlDataSourceStatusEventArgs e)
  103.     {//this is the method that is moody
  104.         int count = e.AffectedRows;
  105.         if (count == 0)
  106.         { msgLabel1.Text = " No Records found"; }
  107.         else
  108.         {
  109.             if (count == 1)
  110.             { msgLabel1.Text = " 1 record found"; }
  111.             else
  112.             { msgLabel1.Text = " " + count + " records found"; }
  113.         }
  114.      }
  115.  
  116.  
  117. }
  118.  
Mar 23 '08 #1
8 2072
I would expect that you could populate this with the afGridView_DataBound method, not the SqlDataSource3_Selected method. I suspect you would then use the datagrid's size / count property to populate your label.

I'm not sure what the datasource selected method would be showing you.

Let us know if this helps.
Mar 24 '08 #2
balabaster
797 Expert 512MB
I would expect that you could populate this with the afGridView_DataBound method, not the SqlDataSource3_Selected method. I suspect you would then use the datagrid's size / count property to populate your label.

I'm not sure what the datasource selected method would be showing you.

Let us know if this helps.
I would expect the DataSource.Selected event to fire after the DataSource is assigned, but before the data is bound, so consequently no data would have loaded at the time this event fires. Consequently the grid would be of zero size. I've found that the grid only has rows and columns after the DataBind event has fired - consequently any grid manipulation pertaining to data held within the grid should occur when or after the DataBind event has fired.
Mar 24 '08 #3
phpmel
69
Thank you so much for your replies.

So in my afGridView_DataBound method, i will use EventArgs "e.what" to get my count?

I am not sure what to do. Can you all please elaborate a little more.
Mar 24 '08 #4
phpmel
69
Thank you so much for your reply.

So in my afGridView_DataBound method, i will use EventArgs "e.what" to get my count?

I am not sure what to do. Can you all please elaborate a little more.
Mar 25 '08 #5
Plater
7,872 Expert 4TB
afGridView.Rows.Count
Mar 25 '08 #6
phpmel
69
Hi,

I have paging enabled in the gridview, so afGridView.rows.count returns 5 since that is my page size. i wanted exactly how many records were returned within the gridview. Do I have to mathematically calculate it using the pagecount and page size, or is there an attribute that i could get it from?
Mar 25 '08 #7
balabaster
797 Expert 512MB
Hi,

I have paging enabled in the gridview, so afGridView.rows.count returns 5 since that is my page size. i wanted exactly how many records were returned within the gridview. Do I have to mathematically calculate it using the pagecount and page size, or is there an attribute that i could get it from?
Don't get hung up on the GridView object - don't forget you have a DataSource object too... that will tell you how many records were returned in total.

Also, your gridview may have 5 pages but only return 3 rows on the last page instead of 5. So your calculation would've had to take that into account 5 x 5 <> (4 x 5) + 3
Mar 25 '08 #8
phpmel
69
I am using the datasource e.Affetcedrows attribute to find out the number of rows that was returned (see first post with code). It is in the SqlDataSource3_Selected method. However, somethimes it is going into the method and sometimes it is not. So because it is so moody i can not ALWAYS get the label to show how much records was returned.

Please ........any suggestions
Mar 27 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
9
by: Timm | last post by:
I have an ASP.NET 2.0 page with two DropDownLists. I am using declarative data binding wherever possible and trying to minimize the use of code. The list of values in DropDownList DDL2 should be...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
0
by: weiwei | last post by:
Hi; I am having trouble to get variable from grid view. here is my scenario. I want to delete a row in database from web page, in additon, I also want to delete that user's directory in c:drive. ...
5
by: Amit | last post by:
Hello, I have a simple search screen, with two drop-downs and a text box. There's also a GridView control that is using a SqlDataSource control to show the matching results. The SqlDataSource uses...
2
by: probashi | last post by:
Hi, Using the SqlDataSource/SelectParameters/ControlParameter one can easily bind a Grid View with a list box (or any other controls), pretty cool, but my list box is multi select. My...
12
by: brwalias | last post by:
Hi, using .net 2 sql server 2005 Here is my situation: I'm passing a variable in the url from a selection on Page A and need to display the results on the Results page be based on that...
0
by: troydixon | last post by:
Hello, I am new at this, and have been trying to insert data into a table by using the footer of a gridview (which I dont like) or by using a detials view on the same page that is doing the...
0
by: teressa | last post by:
Hi, I am new to Grid view Control. I have a page with many icons on it for different purposes. I want to add a Grid View where I want to load data with different columns. Below is my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.