what i'm missing that i get the error: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource' could not be found.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="NewsId" EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="NewsId" HeaderText="NewsId" InsertVisible="False"
ReadOnly="True" SortExpression="NewsId" />
<asp:BoundField DataField="News_DatePosted" HeaderText="News_DatePosted" SortExpression="News_DatePosted"
/>
<asp:BoundField DataField="News_PostedBy" HeaderText="News_PostedBy" SortExpression="News_PostedBy"
/>
<asp:BoundField DataField="News_PostedByName" HeaderText="News_PostedByName" SortExpression="News_PostedByName"
/>
<asp:BoundField DataField="News_Title" HeaderText="News_Title" SortExpression="News_Title"
/>
<asp:BoundField DataField="News_Body" HeaderText="News_Body" SortExpression="News_Body"
/>
</Columns>
</asp:GridView>
protected void btnFilter_Click(object sender, EventArgs e)
{
AccessDataSource AccessDataSource1 = new AccessDataSource();
AccessDataSource1.DataFile = "~/App_Data/RGN.mdb";
AccessDataSource1.SelectCommand = "SELECT * FROM [RGN_News]";
AccessDataSource1.ID = "AccessDataSource";
//Clear any selet parameters
AccessDataSource1.SelectParameters.Clear();
this.GridView1.DataSourceID = AccessDataSource1.ID;
//this.GridView1.DataKeyNames = Convert.ToString("NewsId").ToString();
//Create the SQL Command Text with an appened where clause
String sqlSelectCommand = String.Concat(AccessDataSource1.SelectCommand, " WHERE News_PostedByName LIKE '%'+?+'%'");
//set the SelectCommand to the sqlSelectCommand variable
AccessDataSource1.SelectCommand = sqlSelectCommand;
//Tell the SqlDataSource that we're uisng text as our SQL statement
AccessDataSource1.SelectCommandType = SqlDataSourceCommandType.Text;
//Add the control parameter to the slectParameters collection
AccessDataSource1.SelectParameters.Add(new ControlParameter("News_PostedByName", "txtFilterCriteria", "Text"));
}
Thanks
Its all about coding!
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
The problem is that the DataSourceID property is designed to be used in an ASPX page and you are in the code. You want to use the DataSource property instead. It is looking in your ASPX page for a control named "AccessDataSource" and there is not one. If
you create this access data source object in the ASPX page instead of in the code you will be able to set the DataSourceID to the ID of the AccessDataSource object.
It is entirely possible. What you will want to do instead is to use Data Objects instead of using asp.net controls
this will give you a basic explanation of how to do it not in the code behind http://msdn2.microsoft.com/en-us/library/445z2s49(VS.80).aspx
this article has an example of how to do what you're trying to do from the code behind. Just use your gridview instead of the datagrid http://msdn2.microsoft.com/en-us/library/ms971485.aspx
It is entirely possible. What you will want to do instead is to use Data Objects instead of using asp.net controls
this will give you a basic explanation of how to do it not in the code behind http://msdn2.microsoft.com/en-us/library/445z2s49(VS.80).aspx
this article has an example of how to do what you're trying to do from the code behind.
Just use your gridview instead of the datagrid http://msdn2.microsoft.com/en-us/library/ms971485.aspx
you mean to say, i can do it only if i use DATAGRID ? i'm using gridview
Its all about coding!
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
No, what I mean is that you are completely able to use a gridview. Yes use a gridview. In the example I sent you they use a datagrid. I am just saying that the gridview works just as well. Go for it.
But I hate coding in the html view and often use the code behind page. But I'm receiving odd errors when coding in the code behind page. The code that is giving me errors is below:
----------------------------------------------------------------------
BusObjects objects = new BusObjects();
ObjectSet employees = objects.GetEmployees();
ObjectDataSource objDs = new ObjectDataSource("objects","GetEmployees");
[System.Web.HttpException] = {"The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'objDs1' could not be found."}
Its all about coding!
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
That might work, and if it doesn't we need to teach you methods of getting data other than the datasource objects they are really just designed to be used on aspx pages.
nisarkhan
Contributor
2402 Points
1472 Posts
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ...
Jul 18, 2007 12:51 AM|LINK
what i'm missing that i get the error: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource' could not be found.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="NewsId"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="NewsId" HeaderText="NewsId" InsertVisible="False" ReadOnly="True"
SortExpression="NewsId" />
<asp:BoundField DataField="News_DatePosted" HeaderText="News_DatePosted" SortExpression="News_DatePosted" />
<asp:BoundField DataField="News_PostedBy" HeaderText="News_PostedBy" SortExpression="News_PostedBy" />
<asp:BoundField DataField="News_PostedByName" HeaderText="News_PostedByName" SortExpression="News_PostedByName" />
<asp:BoundField DataField="News_Title" HeaderText="News_Title" SortExpression="News_Title" />
<asp:BoundField DataField="News_Body" HeaderText="News_Body" SortExpression="News_Body" />
</Columns>
</asp:GridView>
protected void btnFilter_Click(object sender, EventArgs e) { AccessDataSource AccessDataSource1 = new AccessDataSource(); AccessDataSource1.DataFile = "~/App_Data/RGN.mdb"; AccessDataSource1.SelectCommand = "SELECT * FROM [RGN_News]"; AccessDataSource1.ID = "AccessDataSource"; //Clear any selet parameters AccessDataSource1.SelectParameters.Clear(); this.GridView1.DataSourceID = AccessDataSource1.ID; //this.GridView1.DataKeyNames = Convert.ToString("NewsId").ToString(); //Create the SQL Command Text with an appened where clause String sqlSelectCommand = String.Concat(AccessDataSource1.SelectCommand, " WHERE News_PostedByName LIKE '%'+?+'%'"); //set the SelectCommand to the sqlSelectCommand variable AccessDataSource1.SelectCommand = sqlSelectCommand; //Tell the SqlDataSource that we're uisng text as our SQL statement AccessDataSource1.SelectCommandType = SqlDataSourceCommandType.Text; //Add the control parameter to the slectParameters collection AccessDataSource1.SelectParameters.Add(new ControlParameter("News_PostedByName", "txtFilterCriteria", "Text")); }Thanks
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
benrick
Contributor
2439 Points
405 Posts
ASPInsiders
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 01:37 AM|LINK
The problem is that the DataSourceID property is designed to be used in an ASPX page and you are in the code. You want to use the DataSource property instead. It is looking in your ASPX page for a control named "AccessDataSource" and there is not one. If you create this access data source object in the ASPX page instead of in the code you will be able to set the DataSourceID to the ID of the AccessDataSource object.
I hope that helps,
Brendan
Brendan's Blog
nisarkhan
Contributor
2402 Points
1472 Posts
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 01:50 AM|LINK
i'm not sure is it possible or not but i want to do it everything from code-behind.
thanks
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
benrick
Contributor
2439 Points
405 Posts
ASPInsiders
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 02:57 AM|LINK
It is entirely possible. What you will want to do instead is to use Data Objects instead of using asp.net controls
this will give you a basic explanation of how to do it not in the code behind http://msdn2.microsoft.com/en-us/library/445z2s49(VS.80).aspx
this article has an example of how to do what you're trying to do from the code behind. Just use your gridview instead of the datagrid http://msdn2.microsoft.com/en-us/library/ms971485.aspx
Brendan's Blog
nisarkhan
Contributor
2402 Points
1472 Posts
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 12:23 PM|LINK
you mean to say, i can do it only if i use DATAGRID ? i'm using gridview
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
benrick
Contributor
2439 Points
405 Posts
ASPInsiders
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 12:46 PM|LINK
No, what I mean is that you are completely able to use a gridview. Yes use a gridview. In the example I sent you they use a datagrid. I am just saying that the gridview works just as well. Go for it.
Brendan's Blog
nisarkhan
Contributor
2402 Points
1472 Posts
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 02:26 PM|LINK
i 'm not using access db, that was a sample i have created to show.
i'm using n-tier
ObjectDataSource obj = new ObjectDataSource();obj.ID =
"myobj";obj.EnableCaching = false;
obj.TypeName = "dal.employee";
obj.SelectMethod = "getEmployee";
the ms links does not talk my problem.
thanks
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
nisarkhan
Contributor
2402 Points
1472 Posts
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 03:10 PM|LINK
But I hate coding in the html view and often use the code behind page. But I'm receiving odd errors when coding in the code behind page. The code that is giving me errors is below:
objDs.SelectParameters.Add(new Parameter("UserId", TypeCode.String, UserId));----------------------------------------------------------------------
BusObjects objects = new BusObjects();
ObjectSet employees = objects.GetEmployees();
ObjectDataSource objDs = new ObjectDataSource("objects","GetEmployees");
objDs.SelectParameters.Add(new ControlParameter("SearchDate", TypeCode.DateTime, "DatePicker1", "SelectedDate")); objDs.ID = "objDs1";
GridView1.DataSourceID = "objDs1";
----------------------------------------------------------------------
[System.Web.HttpException] = {"The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'objDs1' could not be found."}
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
benrick
Contributor
2439 Points
405 Posts
ASPInsiders
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 03:26 PM|LINK
change it to the following and it should work
BusObjects objects = new BusObjects(); ObjectSet employees = objects.GetEmployees(); ObjectDataSource objDs = new ObjectDataSource("objects","GetEmployees"); objDs.SelectParameters.Add(new Parameter("UserId", TypeCode.String, UserId)); objDs.SelectParameters.Add(new ControlParameter("SearchDate", TypeCode.DateTime, "DatePicker1", "SelectedDate")); objDs.ID = "objDs1";Brendan's Blog
benrick
Contributor
2439 Points
405 Posts
ASPInsiders
Re: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control w...
Jul 18, 2007 03:32 PM|LINK
actually now that I read your code more clearly why don't you try this.
Brendan's Blog