473,379 Members | 1,317 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,379 software developers and data experts.

Gridview data populates only after button click.

Ewan
18
Hi

I have a gridview which should populate based on some criteria that the end user selects.

To enable the user to enter the required criteria i have added some Web Controls like Textbox,Drop Down Lists etc.



My code behind for getting the data is as below
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3.  
  4. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.  
  6.         Dim myconnection As SqlConnection
  7.         Dim mydataadapter As SqlDataAdapter
  8.         Dim DS As DataSet
  9.  
  10.  
  11.         If txtSearchRequest.Text = "" Then
  12.             txtSearchRequest.Text = "%"
  13.         End If
  14.  
  15.         myconnection = New SqlConnection("server=TestServer;database=TestDatabase;Trusted_Connection=yes")
  16.         mydataadapter = New SqlDataAdapter("GetData", myconnection)
  17.         mydataadapter.SelectCommand.CommandType = CommandType.StoredProcedure
  18.         'Adding the parameters and assigning the values
  19.  
  20.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@ui_request", SqlDbType.NVarChar, 4000))
  21.         mydataadapter.SelectCommand.Parameters("@ui_request").Value = txtSearchRequest.Text
  22.  
  23.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@ui_process_status", SqlDbType.NVarChar, 4000))
  24.         mydataadapter.SelectCommand.Parameters("@ui_process_status").Value = ddlProcessStatus.Text
  25.  
  26.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@ui_mitigated_status", SqlDbType.NVarChar, 4000))
  27.         mydataadapter.SelectCommand.Parameters("@ui_mitigated_status").Value = ddlMitigatedStatus.Text
  28.  
  29.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@ui_age_slab", SqlDbType.NVarChar, 4000))
  30.         mydataadapter.SelectCommand.Parameters("@ui_age_slab").Value = ddlAgeSlab.Text
  31.  
  32.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@ui_nssr", SqlDbType.NVarChar, 4000))
  33.         mydataadapter.SelectCommand.Parameters("@ui_nssr").Value = ddlStandardNSSR.Text
  34.  
  35.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@ui_owner", SqlDbType.NVarChar, 4000))
  36.         mydataadapter.SelectCommand.Parameters("@ui_owner").Value = ddlOwner.Text
  37.  
  38.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@ui_reassigned_to", SqlDbType.NVarChar, 4000))
  39.         mydataadapter.SelectCommand.Parameters("@ui_reassigned_to").Value = ddlReassignedTo.Text
  40.  
  41.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@ui_tool", SqlDbType.NVarChar, 4000))
  42.         mydataadapter.SelectCommand.Parameters("@ui_tool").Value = ddlTool.Text
  43.  
  44.         mydataadapter.SelectCommand.Parameters.Add(New SqlParameter("@row_count", SqlDbType.Int))
  45.         mydataadapter.SelectCommand.Parameters("@row_count").Direction = ParameterDirection.Output
  46.  
  47.  
  48.         DS = New DataSet()
  49.         mydataadapter.Fill(DS, "mydata")
  50.  
  51.         lblGetCount.Text = DS.Tables(0).Rows.Count().ToString()
  52.         lblGetCount.Text = mydataadapter.SelectCommand.Parameters("@row_count").Value & " Records Found!"
  53.         gridview1.DataSource() = DS.Tables("mydata").DefaultView
  54.         gridview1.DataBind()
  55.  
  56.         mydataadapter.Dispose()
  57.         myconnection.Close()
  58.  
  59.  
  60.     End Sub
  61.  

Here the value from each Textbox or Drop Down List is assigned to the SQL Parameter. which is passed on to the Stored Procedure "GetData"

My issue is that when the page loads i want all the records to be displayed.
However this is not the case. The records get populated in the Gridview only if i click on a Button.

Could anyone advise me how the same could happen at the Page_Load stage, or what could be causing the issue.
Attached Images
File Type: jpg Filters.jpg (15.0 KB, 1969 views)
Mar 25 '11 #1
8 4664
kadghar
1,295 Expert 1GB
a couple of IFs should do:

Expand|Select|Wrap|Line Numbers
  1. If Parameter1 <> nothing then
  2.     'your code for dynamic SQL
  3. Else
  4.     'your Data Adapter for all entries
  5. End If
Mar 25 '11 #2
Ewan
18
Hi kadghar,
Thanks for your reply. i have infact handled this in the SQL Procedure, and this works fine.

However my concern is that when the page loads, the gridview is not populated. Only when i click the search button it works. Im guessing this is trigrering a Post Back, and data is displayed only after the post back occurs.

My concern is : how do i get this to display when the page is loaded.
Mar 28 '11 #3
kadghar
1,295 Expert 1GB
At a first glance, your code in your Load event seems right. But are you sure you're retrieving the info? are you calling the right params to retrieve alll the info when there are actually no user parameters?
Mar 28 '11 #4
Ewan
18
Hi kadghar

Thanks foy your reply

yes i am updating all parameters to get the data.

pasting my SQL Code (Procedure) below

Expand|Select|Wrap|Line Numbers
  1. set ANSI_NULLS ON
  2. set QUOTED_IDENTIFIER ON
  3. go
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. ALTER PROCEDURE [dbo].[GetHPSMQuestData]
  13.     @ui_request NVARCHAR (4000),
  14.     @ui_process_status NVARCHAR (4000),
  15.     @ui_mitigated_status NVARCHAR (4000),
  16.     @ui_age_slab NVARCHAR (4000),
  17.     @ui_nssr NVARCHAR (4000),
  18.     @ui_owner NVARCHAR (4000),
  19.     @ui_reassigned_to NVARCHAR (4000),
  20.     @ui_tool NVARCHAR (4000),
  21.     @row_count INT OUTPUT
  22.  
  23.  
  24. AS
  25. DECLARE
  26.     @fi_request NVARCHAR (4000),
  27.     @fi_process_status NVARCHAR (4000),
  28.     @fi_mitigated_status NVARCHAR (4000),
  29.     @fi_age_slab NVARCHAR (4000),
  30.     @fi_nssr NVARCHAR (4000),
  31.     @fi_owner NVARCHAR (4000),
  32.     @fi_reassigned_to NVARCHAR (4000),
  33.     @fi_tool NVARCHAR (4000),
  34.     @select NVARCHAR(4000),
  35.     @cols_part1 NVARCHAR(4000),
  36.     @cols_part2 NVARCHAR(4000),
  37.     @where NVARCHAR (4000)
  38.  
  39.  
  40. --set the parameters
  41. SET @fi_request = 'LIKE '+''''+@ui_request+''''
  42.     IF @ui_request = '%'
  43.         SET @fi_request = 'LIKE '+'''%'''
  44.  
  45. SET @fi_process_status = 'LIKE '+''''+@ui_process_status+''''
  46.     IF @ui_process_status = '%'
  47.         SET @fi_process_status = 'LIKE '+'''%'''
  48.  
  49. SET @fi_mitigated_status = 'LIKE '+''''+@ui_mitigated_status+''''
  50.     IF @ui_mitigated_status = '%'
  51.         SET @fi_mitigated_status = 'LIKE '+'''%'''
  52.  
  53. SET @fi_age_slab = 'LIKE '+''''+@ui_age_slab+''''
  54.     IF @ui_age_slab = '%'
  55.         SET @fi_age_slab = 'LIKE '+'''%'''
  56.  
  57. SET @fi_nssr = 'LIKE '+''''+@ui_nssr+''''
  58.     IF @ui_nssr = '%'
  59.         SET @fi_nssr = 'LIKE '+'''%'''
  60.  
  61. SET @fi_owner = 'LIKE '+''''+@ui_owner+''''
  62.     IF @ui_owner = '%'
  63.         SET @fi_owner = 'LIKE '+'''%'''
  64.  
  65. SET @fi_reassigned_to = 'LIKE '+''''+@ui_reassigned_to+''''
  66.     IF @ui_reassigned_to = '%'
  67.         SET @fi_reassigned_to = 'LIKE '+'''%'''
  68.  
  69. SET @fi_tool = 'LIKE '+''''+@ui_tool+''''
  70.     IF @ui_tool = '%'
  71.         SET @fi_tool = 'LIKE '+'''%'''
  72.  
  73.  
  74. --build the SQL statement
  75. SET @select =        'SELECT '
  76. SET @cols_part1 = 
  77.                     'CONVERT(NVARCHAR,[wr_picked],3) as [Picked],
  78.                     [request] as [Request],
  79.                     [description] as [Description],
  80.                     [WRE],
  81.                     [OpCo],
  82.                     [delivery_days] as [Age],
  83.                     [age_group] as [Age Group],
  84.                     [NSSR],[OWNER],
  85.                     [escalation_status] as [Escalation Status],
  86.                     [escalated_to(specific_name)] as [Escalated To],
  87.                     CONVERT(NVARCHAR,[escalated_date_to_RG],3) AS [Escalated Date],'
  88. SET @cols_part2 =
  89.                     '[resolver_teams] as [Resolver Team],
  90.                     CONVERT(NVARCHAR,[ETA_provided],3) as [ETA],
  91.                     [Customer_Affecting] as [Customer Affecting],
  92.                     CONVERT(NVARCHAR,[date_last_update_given_to_requester/user],3) as [Last Update Given],
  93.                     [reason_for_delay] as [Delay Reason],
  94.                     convert(NVARCHAR(100),[action_taken])+''...''as [Agent Comments],
  95.                     [delay_cat] as [DelayCatagory],
  96.                     [Tool] 
  97.                     from dbo.test_environ_hpsm '
  98. SET @where =        'WHERE request '+ @fi_request+
  99.                     ' AND ISNULL(process_status,'''') '+ @fi_process_status+
  100.                     ' AND ISNULL(mitigated_status,'''') '+ @fi_mitigated_status+
  101.                     ' AND ISNULL(age_group,'''') '+ @fi_age_slab+
  102.                     ' AND ISNULL(NSSR,'''') '+ @fi_nssr+
  103.                     ' AND ISNULL(OWNER,'''') '+ @fi_owner+
  104.                     ' AND ISNULL(reassigned_to,'''') '+ @fi_reassigned_to+
  105.                     ' AND ISNULL(Tool,'''') '+ @fi_tool
  106. --run the SQL Statement
  107. EXECUTE (@select+@cols_part1+@cols_part2+@where)
  108.  
  109. SELECT @row_count = @@ROWCOUNT
  110.  
I have updated the frontend data to select % as a default parameter (on first load) for all the filters. (Same as the pic attached)
If a field carries %, this gets handled accordingly in the SQL Procedure.

Getting the data is not the issue.. the issue is the data does not populate at on page load, it populates only after the Search button (shown in the attached pic) is clicked.
Mar 30 '11 #5
kadghar
1,295 Expert 1GB
I see, why don't you put a breakpoint in the Load event and allow debuging, just to see if the SUB is running complete an correctly?

Perhaps the issue is with te IF/THEN you put for txtSearchRequest.Text = ""
Mar 30 '11 #6
Ewan
18
Hi kadghar

Tried the breakpoint and debugging option. The Sub runs without any errors.
Mar 31 '11 #7
Ewan
18
Hi I managed to get a fix to this.
Not sure if this is the best option to choose, but it works.

The issues was, the databind for filters were happening together with gridview databind , so posibilities are that correct/all parameters are not passed on to the SQl Procedure to get the data as required and all required parameters get passed on correctly when the Search button is clicked(This is what i presumed).

So i update the code in Page_PreInit to bing the filters before the page_load event, so in page load event the parameters get passed on as required.

Expand|Select|Wrap|Line Numbers
  1.     Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
  2.         If Not IsPostBack Then
  3.             ddlAgeSlab.DataBind()
  4.             ddlProcessStatus.DataBind()
  5.             ddlMitigatedStatus.DataBind()
  6.             ddlStandardNSSR.DataBind()
  7.             ddlOwner.DataBind()
  8.             ddlTool.DataBind()
  9.             ddlReassignedTo.DataBind()
  10.         End If
  11.     End Sub
  12.  
Mar 31 '11 #8
kadghar
1,295 Expert 1GB
Thats a good solution. ^.^
Or you could just simple put the "empty" parameters when is not posting back on the load form.
Mar 31 '11 #9

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

Similar topics

0
by: Jim Bayers | last post by:
Currently, users click on a button to download data. They click and wait patiently for a minute for the sql server to send them their data. Problem is, some users keep clicking on the button over...
2
by: Dan | last post by:
I want a user to be able to click a button on one webform, and if the fields on that form are filled in properly, I want the app to load another webform in the same virtual directory. How would I...
1
by: Rich | last post by:
Hello, I have a form with a panel which contains a radiobutton. When I click the radiobutton, I invoke the Paint event of the panel using me.Invalidate. The paint event gets called and runs...
3
by: John Devlon | last post by:
Hi .. Can anyone please help me ? Ik would like to raise a messagebox after the close-icon is clicked, asking me if I'm sure to quit ... I'm using this code ... Private Sub...
2
by: ind_123 | last post by:
I have a GridView which is empty and doesnt display on page_load. I have a button outside of GridView that binds it to a SqlDataSource, dataset fill. On Postback I dont see the Grid View. The page...
4
by: mohaaron | last post by:
This seems like it should be simple to do but for some reason I have been unable to make it work. I would like to databind a SqlDataSource to a GridView during the click event of a button. This...
3
by: Andrew Jocelyn | last post by:
Hi I have couple of dropdown controls each with a requiredfieldvalidator. I clear the dropdownlist in the button click event. When the page is rendered the dropdown validator already flags it's...
4
by: sreemathy2000 | last post by:
I have a vague problem.. Inmy windows application in c#, i'm opening a child window from from my main window thru the show method. I have few events on the child window. After my button click...
1
by: coreyk | last post by:
I have been trying for ever to hide a column in gridview using the following code, GridView1.Columns.Visible = false; but the debugger says that index is out of range, the number columns when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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?

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.