如何以编程方式更改sqldatasource [英] How to change the sqldatasource programmatically
本文介绍了如何以编程方式更改sqldatasource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将Sqldatasource用于Gridview.我想在运行时更改数据源的操作方法,请帮帮我.
我在设计页面上给出以下命令
I am using Sqldatasource for Gridview. I want to change the datasource at runtime how to do it please help me.
I give the following command at design page
<asp:GridView ID="GridView2" OnRowDataBound="GridView2_RowDataBound" DataSourceID="SqlDataSource1" runat="server" align="center" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" Height="161px" Width="837px">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
我想更改以下代码的数据源选择命令
And I want to Change the datasource select command for below code
SqlCommand sqlcmd = new SqlCommand("FETCH_COUNTER_UMAP_SP",connect.cn());
sqlcmd.Parameters.AddWithValue("@COMPANYCODE", company_name);
sqlcmd.Parameters.AddWithValue("@BRANCHCODE", s);
sqlcmd.Parameters.AddWithValue("@USERID", uname);
sqlcmd.Parameters.AddWithValue("@FINYEAR", Finyear);
sqlcmd.Parameters.AddWithValue("@ERRORID", 1);
sqlcmd.Parameters.AddWithValue("@MAPUSERID", cmbusers.SelectedValue);
sqlcmd.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
SqlDataSource1.SelectCommand = dt;
SqlDataSource1.DataBind();
但它显示错误如何帮助我
But it is display Error how do it please help me
推荐答案
您好,
试试这个:
Hi,
Try this:
SqlDataSource1.DataSourceMode=SqlDataSourceMode.DataReader;
SqlDataSource1.SelectCommand = "select * from table1 where name='"+txtName.Text+"'";
SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString;
//Now rebind the grid;
GridView2.DataSource = SqlDataSource1;
GridView2.DataBind();
// Changing sqldatasouce to datatable
//create dataview from sql data source
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
//convert dataview to datatable
DataTable dt = dv.ToTable();
//Now dt is the table with all the values.
--Amit
--Amit
这篇关于如何以编程方式更改sqldatasource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文