Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL-Server
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
Posted 31-Jul-12 20:59pm
devausha1.4K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v4
Comments
devausha at 1-Aug-12 2:14am
   
Thank you for your reply. If I used datatable or datareader like in questions, How I assign it? Please help me.
_Amy at 1-Aug-12 2:32am
   
Try my updated answer.
devausha at 1-Aug-12 2:58am
   
Thank you for your reply. I want how to set the datasource of the Sqldatasource1 to dt or ds
_Amy at 1-Aug-12 3:27am
   
Commands are required for that. Use commands. Try reading this:
http://msdn.microsoft.com/en-us/library/dz12d98w%28v=vs.80%29.aspx[^]
devausha at 1-Aug-12 5:09am
   
Thank you this link is very useful
_Amy at 1-Aug-12 5:47am
   
Welcome. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 235
1 DamithSL 225
2 OriginalGriff 198
3 George Jonsson 170
4 Maciej Los 165
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100