Question : Stored Procedure and GridView - CommandText Vs StoredProcedure

I am looking for ways to improve performance when data is loaded onto the grid view control. I am considering how to implement stored procedures with gridview control. (C# , VS2008, MSSQL 2005)

The problem I have with using the stored procedure command type to get data is that it does not support server-side paging and only works when paging is set to false on the GridView control. (See code - option 1)

To get around this I have take another option. This is to run the procedure using "EXEC ' command which is actually supplied in the command text property of the sqladapter (see code below - option 2). This allows paging when the data is returned.

However, I am not confident that Option 2 actually performs better. We are dealing with small amounts of data on the development server and they 'appear' to perform the same.

Reading the 2 sets of code.
1. Does option 2 perform better than option one.?
2. If not so then is option 2 better when I use the 'EXEC' .. and stored procedure instedad of using full 'Select' statement in the command text with no stored procedure?
3. Is there another way of doing this better outside of my options 1 and 2.?
4. How can I set up a performance counter to validate which performs best.


Please help.

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
//The stored procedure is as follows
 
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetAccountCode]
@CompCode int,
@AccountTypeID int
As
select AccountCode as Col_01,AccountName as Col_02,left(AccountName,20) as Col_03,AccountTypeID as Col_04,AccountStatusID as Col_05
from RefAccount
where CompCode = @CompCode and AccountTypeID = @AccountTypeID
order by AccountTypeID,AccountCode
 
 
//This is the option 1  that uses stored procedure command Type:
 
            SqlCommand cmd = new SqlCommand("GetAccountCode", cnDBAAA);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
 
            cmd.Parameters.AddWithValue("@CompCode", CompCode);
            cmd.Parameters.AddWithValue("@AccountTypeID", AccountTypeID);
 
            try
            {
 
                cnDBAAA.Open();
 
                //Set datasource and bind table data to gridview.
                dgSelection.DataSource = cmd.ExecuteReader();
                dgSelection.DataBind();
 
                //Clear datasource ready for use in next gridview data
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
            catch (Exception)
            {
               
            }
            finally
            {
                cnDBAAA.Close();
            }
 
 
//This is option 2 which in which i use a sql adapter with fill method to populate grid view data.
 
           SQLString = "EXEC GetAccountCode " + CompCode + "," + AccountTypeID;
 
            try
            {
                SqlDataAdapter sdaDBAAA = new SqlDataAdapter(SQLString, cnDBAAA);
                cnDBAAA.Open();
                sdaDBAAA.Fill(dsCodes);
 
                //Set datasource and bind table data to gridview.
                dgSelection.DataSource = dsCodes;
                dgSelection.DataBind();
 
                //Clear datasource ready for use in next gridview data
                dsCodes.Clear();
 
            }
            catch (Exception)
            {
            }
            finally
            {
                cnDBAAA.Close();
            }
 
 
//The skin id for the Grid

    
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
    
    
    

Answer : Stored Procedure and GridView - CommandText Vs StoredProcedure

If you use paging in your gridview and want ot improve preformance, i strongly suggest to look at costum paging instead of doing what you are trying to do (so it`s 3)


here a link that explain this mecanism and how ithttp://aspnet.4guysfromrolla.com/articles/031506-1.aspx  

Feel free to ask if you need more info.

Random Solutions  
 
programming4us programming4us