The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource' could not be found.

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>

  
1    protected void btnFilter_Click(object sender, EventArgs e)
2        {
3    
4            AccessDataSource AccessDataSource1 = new AccessDataSource();
5            AccessDataSource1.DataFile = "~/App_Data/RGN.mdb";
6            AccessDataSource1.SelectCommand = "SELECT * FROM [RGN_News]";
7            AccessDataSource1.ID = "AccessDataSource";
8            //Clear any selet parameters
9            AccessDataSource1.SelectParameters.Clear();
10   
11           this.GridView1.DataSourceID = AccessDataSource1.ID;
12           //this.GridView1.DataKeyNames =  Convert.ToString("NewsId").ToString();
13           //Create the SQL Command Text with an appened where clause
14           String sqlSelectCommand = String.Concat(AccessDataSource1.SelectCommand, " WHERE News_PostedByName LIKE '%'+?+'%'");
15   
16           //set the SelectCommand to the sqlSelectCommand variable
17           AccessDataSource1.SelectCommand = sqlSelectCommand;
18   
19           //Tell the SqlDataSource that we're uisng text as our SQL statement
20           AccessDataSource1.SelectCommandType = SqlDataSourceCommandType.Text;
21   
22           //Add the control parameter to the slectParameters collection
23           AccessDataSource1.SelectParameters.Add(new ControlParameter("News_PostedByName", "txtFilterCriteria", "Text"));
24       }

 

Thanks


Its all about coding!
0
nisarkhan
7/18/2007 12:51:00 AM
asp.net.access-datasource 4679 articles. 0 followers. Follow

17 Replies
2279 Views

Similar Articles

[PageSpeed] 36
Get it on Google Play
Get it on Apple App Store

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 


C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
7/18/2007 1:37:32 AM

i'm not sure is it possible or not but i want to do it everything from code-behind.

thanks


Its all about coding!
0
nisarkhan
7/18/2007 1:50:57 AM

 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 


C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
7/18/2007 2:57:59 AM

benrick:

 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!
0
nisarkhan
7/18/2007 12:23:42 PM

 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.


C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
7/18/2007 12:46:05 PM

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


Its all about coding!
0
nisarkhan
7/18/2007 2:26:23 PM

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");

objDs.SelectParameters.Add(new Parameter("UserId", TypeCode.String, UserId));
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."}

 


Its all about coding!
0
nisarkhan
7/18/2007 3:10:25 PM

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";
this.Page.Form.Controls.Add(objDs);
GridView1.DataSourceID = "objDs1";
 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. 

C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
7/18/2007 3:26:22 PM

 actually now that I read your code more clearly why don't you try this.

BusObjects objects = new BusObjects();
ObjectSet employees = objects.GetEmployees();

GridView1.DataSource = employees;
GridView1.DataBind();

C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
7/18/2007 3:32:49 PM

benrick:

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";
this.Page.Form.Controls.Add(objDs); <<<<<<<<<<<<<error
GridView1.DataSourceID = "objDs1";
 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. 

error: the Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

 

 


Its all about coding!
0
nisarkhan
7/18/2007 3:42:45 PM

benrick:

 actually now that I read your code more clearly why don't you try this.

BusObjects objects = new BusObjects();
ObjectSet employees = objects.GetEmployees();

GridView1.DataSource = employees;
GridView1.DataBind();



i can not use : GridView1.DataSource = employees;
 
because employee expects parameters


Its all about coding!
0
nisarkhan
7/18/2007 3:44:41 PM

 try putting a Panel right before the gridview and change

this.Page.Form.Controls.Add(objDs);
to
myPanel.Controls.Add(objDs); 

 


C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
7/18/2007 3:47:42 PM

benrick:

 try putting a Panel right before the gridview and change

this.Page.Form.Controls.Add(objDs);
to
myPanel.Controls.Add(objDs); 



here is what i did:

<asp:Panel ID="mypanel" runat="server" >

<asp:UpdatePanel ID="upnlFacilities" runat="server">

<ContentTemplate>

<asp:GridView AllowSorting="True" AllowPaging .....
.................................

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"));


obj.ID = "objDs1";

 

Panel mypanel = (Panel)Page.FindControl("mypanel");

// this.Page.Form.Controls.Add(obj);

GridView.DataSourceID = obj.ID;

mypanel.Controls.Add(obj);


[System.InvalidOperationException] = {"ObjectDataSource 'objDs1' could not find a non-generic method 'GetEmployees' that has parameters: UserId, SearchDate"}
 

Its all about coding!
0
nisarkhan
7/18/2007 4:02:19 PM
try changing this line
ObjectDataSource objDs = new ObjectDataSource("BusObjects","GetEmployees");

 


C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
7/18/2007 4:16:49 PM

benrick:
try changing this line
ObjectDataSource objDs = new ObjectDataSource("BusObjects","GetEmployees");

 

its returning me the data now :)

but

when i do sorting i end-up in one more problem:

Message = "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!
0
nisarkhan
7/18/2007 4:22:54 PM

sadly for that code to work you will need to run it every time not just when it is not a postback 


C. Brendan Enrick
Brendan's Blog

Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
0
benrick
7/18/2007 5:25:48 PM

i realized that.

thank you so much for all your help.

 


Its all about coding!
0
nisarkhan
7/18/2007 6:03:39 PM
Reply:

Similar Artilces:

The DataSourceID of 'gv2' must be the ID of a control of type IDataSource. A control with ID 'objDs1' could not be found.
i have a radiobutton list and gridview and a object data source at runtime i'm changing the object data source by clicking on the radiobutton list selected index changed and here is my source code....          <asp:RadioButtonList runat="server" ID="rblView" RepeatDirection="Horizontal" RepeatLayout="Flow" AutoPostBack="True" OnSelectedIndexChanged="rblView_SelectedIndexChanged">                &nb...

The DataSourceID of 'DetailsView1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource1' could not be found.
 Anyone help with this? i have tried lots of ways to query this database and create the insert command, including making my own data sets and setting it up that way with my own custom SQL statement.....this did not work, so i though i would use VWD 2008's own tools to create the insert, i have been left with the following error: The DataSourceID of 'DetailsView1' must be the ID of a control of type IDataSource.  A control with ID 'AccessDataSource1' could not be found.   Everything looks ok to me. Code below:   <%@ Page Title="" Lan...

How do fix the error The DataSourceID of 'DataGrid1' must be the ID of a control of type IDataSource. A control with ID 'SqlMedTable' could not be found.
How do you fix the error: The DataSourceID of 'DataGrid1' must be the ID of a control of type IDataSource.  A control with ID 'SqlMedTable' could not be found.Here is the code.protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) {MedicalClassLibrary.Connection c = new MedicalClassLibrary.Connection(); c.GetMedicalTable();DataGrid1.AutoGenerateColumns = true; DataGrid1.DataSource = c.DS;InputDiv.Visible = false;Label1.Visible = false; txtRx.Focus(); } } Here is the class medhod:public void GetMedicalTable() {string medTable = "SELECT ...

The DataSourceID of 'ddlDevice' must be the ID of a control of type IDataSource. 'dsMain' is not an IDataSource.
I seem to be having trouble binding the objects in my page to the datasource. The subject line contains the error message that I keep receiving.  I am trying to bind a dropdownlist with my datasource.  I noticed that if I click on the droplist, go to the properties window and try to select a datasource; nothing is listed. Post the code for filling dsMainNC... How did you setup the datasrouce? I think you didnt use the wizard to connect into database. You will not see any datasrouce in the properties window till u use the wizard to (server explorer) create the databa...

The DataSourceID of '' must be the ID of a control of type IDataSource
I have a grid view with dropdown list. I have to be able to edit it by clicking on ‘Edit’ link button. Dropdown at this point should be populated with data.1. I need to display one of the values of the dropdown in the grid (read only). This value is what is coming to populate entire row of the grid view. Even I do not have an error on initial load of grid view and while debugging I see a data coming to populate grid view row and a dropdown, the column is empty. How can I populate a value of the grid view column of the row as a selected field of the dropdown? 2. I am getting an error when l...

DataSourceID of 'controlname' must be the ID of a control of type IDataSource.
When I use the following code, I get this error:The DataSourceID of 'PSAnswersGridView' must be the ID of a control of type IDataSource.  A control with ID 'Key_Trait_Analysis_ObjectDataSource' could not be found.I am trying to define an objectdatasource in code-behind and bind it to a gridview:Partial Class Admin_StuffInherits System.Web.UI.PageProtected Key_Trait_Analysis_ObjectDataSource As ObjectDataSourceProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadIf Not Page.IsPostBack Then...  Key_Trait_Analysis_ObjectDataSource...

Target control with ID '' could not be found for extender '_AccordionExtender'.
Hey Folks, I'm trying to use the accordian in a page that uses a master page and I'm receiving this error.  I've place the following code inside the content place holder tag on the page.<atlasToolKit:Accordion runat='server' SelectedIndex='0' ID="Accordion1" AutoSize="None"FadeTransitions="true"TransitionDuration="250"FramesPerSecond="40"><Panes><atlasToolKit:AccordionPane><Header>Pane 1 </Header><Content> This is pane 1</Content></atlasToolKit:AccordionPa...

''''''''''''''''''''
Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/1.7.2.0 GranParadiso/3.0a1 From URL: http://www.mozilla.org/projects/granparadiso/ Note to readers: Hendrix gives...

The TargetControlID of 'ModalPopupExtender1' is not valid. A control with ID 'Button2' could not be found.
 help.. i dont know what to do with this error... i've been using this on my other pages and it works fine.. but when i copy the code to new page... i get this error... but the button exists and recognized...here is my code... <ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0"> <ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="Receiving Report Info" Width="100%"> <ContentTempla...

error: The TargetControlID of '' is not valid. A control with ID 'txtDate' could not be found.
I want to dynamically create webcontrols (depending on the status of another webcontrol). The code snippet for creating dynamically the webcontrols is hereunder. But when running the code I get the message The TargetControlID of '' is not valid. A control with ID 'txtDate' could not be found.. What am I doing wrong here? Thanks for the help. protected void Page_Load(object sender, EventArgs e) { Label lblDate = new Label(); TextBox txtDate = new TextBox(); ImageButton imgCalendar = new ImageButton(); AjaxControlToolkit.CalendarExtender extender = new AjaxControlToolkit.CalendarE...

A control with ID 'LinkButton1' could not be found for the trigger in UpdatePanel 'UpdatePanelFrmHistory'
Hello mate, i would like to use the update panel for my datagrid  to fire an event handlar method when a link button in the Template column of datagrid is clicked   Here is my code <ajax:UpdatePanel runat="server" ID="UpdatePanelFrmHistory" UpdateMode="Conditional" ><ContentTemplate > <asp:DataGrid ID="DataGrid2" runat="server" ShowHeader="False" OnPageIndexChanged ="Grd_frm_history_OnPageIndexChanged" OnItemDataBound ="DataGrid2_ItemDataBound" style="vertical-align: top; text-a...

Target control with ID 'Panel4' could not be found for extender 'CollapsiblePanelExtender1'
hello when i use collapseble panel control geting erros like 'Target control with ID 'Panel4' could not be found for extender 'CollapsiblePanelExtender1' i am using panels inside <div> please help me     Please see: http://forums.asp.net/thread/1293293.aspx#18Don't forget, this posting is provided "AS IS" with no warranties, and confers no rights. ...

A control with ID 'ImageButton5' could not be found for the trigger in UpdatePanel 'up1'.
I have 20 ImageButtons on my page and UpdatePanel . In Update Panel Triggers i've added all this buttons  <asp:AsyncPostBackTrigger ControlID="IB1" /> <asp:AsyncPostBackTrigger ControlID="ImageButton1" /><asp:AsyncPostBackTrigger ControlID="ImageButton2" /> <asp:AsyncPostBackTrigger ControlID="ImageButton3" /> <asp:AsyncPostBackTrigger ControlID="ImageButton4" /> <asp:AsyncPostBackTrigger ControlID="ImageButton5" /> Here I am Getting this Message A control with ID 'ImageButt...

'''''
Name: mario Email: ramar17atfastwebnetdotit Product: Gran Paradiso Alpha 2 Summary: ''''' Comments: K: Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a2) Gecko/20070206 GranParadiso/3.0a2 ...

Web resources about - The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'AccessDataSource' could not be found. - asp.net.access-datasource

Resources last updated: 11/29/2015 12:11:22 AM