Binding GridView with code-behind

  • Thread starter Thread starter sck10
  • Start date Start date
S

sck10

Hello,

I have a GridView that is using the following to connect to a SQL Server
2000 stored procedure:

<asp:SqlDataSource
ID="dsWebDocList" SelectCommand="sp_web_WebDocument"
ConnectionString="<%$ ConnectionStrings:cnnSQL_Connect %>" Runat="server">
<SelectParameters>
<asp:Parameter Type="String" DefaultValue="FindWebDocAll"
Name="strParm01"></asp:Parameter>
<asp:Parameter Type="String" DefaultValue="Interlock"
Name="strParm02"></asp:Parameter>
<asp:Parameter Type="String" DefaultValue="1-1-2000"
Name="strParm03"></asp:Parameter>
<asp:Parameter Type="String" DefaultValue="NoParameter"
Name="strParm04"></asp:Parameter>
<asp:Parameter Type="String" DefaultValue="NoParameter"
Name="strParm05"></asp:Parameter>
</SelectParameters>
</asp:SqlDataSource>

But for all my other controls, I am using code-behind and using something
like the following:


'Populate the GridView combo box
'------------------------------
Dim spGridView As OleDb.OleDbDataReader
Dim prmGridView As OleDbParameter
Dim cmdGridView As New OleDb.OleDbCommand("sp_web_WebDocument",
cnnSearch)
cmdGridView.CommandType = CommandType.StoredProcedure

'Declare Parameters
prmGridView = cmdGridView.Parameters.Add("@strParm01",
OleDbType.VarChar) : prmGridView.Value = "FindWebDocAll"
prmGridView = cmdGridView.Parameters.Add("@strParm02",
OleDbType.VarChar) : prmGridView.Value = "Interlock"
prmGridView = cmdGridView.Parameters.Add("@strParm03",
OleDbType.VarChar) : prmGridView.Value = "1-1-2000"
prmGridView = cmdGridView.Parameters.Add("@strParm04",
OleDbType.VarChar) : prmGridView.Value = "NoParameter"
prmGridView = cmdGridView.Parameters.Add("@strParm05",
OleDbType.VarChar) : prmGridView.Value = "NoParameter"

'Data Bind: DropdownList
spGridView = cmdGridView.ExecuteReader()

The problem I am having is that I can't bind the data to the GridView. I am
using the following:

'Data Bind: DropdownList
spGridView = cmdGridView.ExecuteReader()
gvWebDocList.DataSourceID = spGridView
gvWebDocList.DataBind()

Any help with this would be appreciated.

Thanks in advance, sck10
 
Hi sck10,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Sck10,

From your description and the code snippet you provided, it seems that
you're assigning the retrieved DataReader object to the DropDownList's
datasource, yes? If so, I think we should try binding the DataReader to the
GridView directly rather than the dropdownlist, the dropdownlist is just a
helper control for selecting tables. The actual datasource should be the
GRidView's DataSource, we should bind the data we retrieved to the
GridView. You may have a try to see whether this is the cause. Also, if
you meet any further problem, you can also have a look in the .net's
Whidbey newsgroup for some more specific guides:

http://communities.microsoft.com/newsgroups/default.asp?icp=whidbey&slcid=us

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Steve,

The problem I am having is trying to find the syntac for binding a GridView
using CodeBehind. I am unable to set the DataSourceID to the
OleDbDataReader. Am I setting the wrong method(?)?

Thanks, Steven


'Populate the GridView combo box
'------------------------------
Dim spGridView As OleDb.OleDbDataReader
Dim prmGridView As OleDbParameter
Dim cmdGridView As New
OleDb.OleDbCommand("sp_web_WebDocument",cnnSearch)
cmdGridView.CommandType = CommandType.StoredProcedure

'Declare Parameters
prmGridView = cmdGridView.Parameters.Add("@strParm01",OleDbType.VarChar)
: prmGridView.Value = "FindWebDocAll"
prmGridView = cmdGridView.Parameters.Add("@strParm02",OleDbType.VarChar)
: prmGridView.Value = "Interlock"
prmGridView = cmdGridView.Parameters.Add("@strParm03",OleDbType.VarChar)
: prmGridView.Value = "1-1-2000"
prmGridView = cmdGridView.Parameters.Add("@strParm04",OleDbType.VarChar)
: prmGridView.Value = "NoParameter"
prmGridView = cmdGridView.Parameters.Add("@strParm05",OleDbType.VarChar)
: prmGridView.Value = "NoParameter"

'Data Bind: GridView
spGridView = cmdGridView.ExecuteReader()
gvWebDocList.DataSourceID = spGridView
gvWebDocList.DataBind()
 
Hi Sck10,

Thank for your response. From my research, the ASP.NET 2.0's GridView
Control is different from the DataGrid/DataList Controls in asp.net 1.1.
The GridView must stand upon a DataSourceControl , so I think if we want to
programmly bind data to GridView Control, we need to programly create a new
DAtaSourceControl( add into the page) first and assign the
DataSourceControl's ID to that GridView Control.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
I am having an issue that is similar.

I have a page that needs to build a complex where clause and return the
results into a gridview. After i bind the data no results are returned,
even when the sql statement (a select statement) returns data. I have
tried something similar with an objectdatasource also but it shows the
same thind.

Here is my code

// m_sds is a sqldatasource object, with the connection, and mode
(dataset) set declaritively
// m_gv is my gridview control
m_sds.SelectCommand = sql;
m_sds.DataSourceMode = SqlDataSourceMode.DataSet;
m_gv.DataSourceID = m_sds.ID;
m_gv.DataBind();
refresh_page();
 
Back
Top