ASP.NET EnqityDataSource WhereParameters, creates new property - asp.net

I am trying to populate GridView, using EntityDataSource(code behind), I need to able to sort GridView. However when I sort i get error:
A property with name 'aspnet_Users.UserId1' does not exist in metadata for entity type
So I beleive it is because I generate where parameter in code behind:
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Full code is :
ActiveEnqDataSource.ConnectionString = db.Connection.ConnectionString;
ActiveEnqDataSource.DefaultContainerName = "Entities";
ActiveEnqDataSource.EntitySetName = "Enquiries";
ActiveEnqDataSource.Include = "UserCars.CarModel.CarMake, Category, aspnet_Users";
ActiveEnqDataSource.EnableUpdate = true;
ActiveEnqDataSource.EnableInsert = true;
ActiveEnqDataSource.EnableDelete = true;
ActiveEnqDataSource.AutoGenerateWhereClause = true;
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Any suggestions? Thank you very much! The gridview itself renders perfectly, only thing I cannot sort it, any "whereParameters" I add, Add 1 to the property e.g UserId1,EnquiryStatus1, ProdauctName1. etc...

I got a similar error because I was adding the where clause each time the page was posted back. Dropping my code that generated the where clause inside an IsPostback statement fixed the problem:
if (!IsPostBack) {
// code to add where parameters
}

I got the same error when I used markup to define a Where parameter AND then I added the same parameter in code. Somewhere along the line, the 1 at the end of the parameter name was added.

Related

Binding database data to a dropdownlist

I have been trying for a long time to fix the bugs I'm encountering with my dropdownlists in my project. I'm getting data this with at the moment and it's working fine:
using (InfoEntities ie = new InfoEntities())
{
var sqlddl = (from query in ie.Table_Customers
from q in ie.Accounting_PriceType
where query.TypeID == 1 && q.ID == 1
orderby query.Customers
select query).Distinct().ToList();
DropDownListCust.DataTextField = "Customers";
DropDownListCust.DataValueField = "ID";
DropDownListCust.DataSource = sqlddl;
DropDownListCust.DataBind();
}
Now when the user saves the data and opens the website again I need the saved value that was chosen on the dropdownlist earlier retreived. This also works fine but the problem is I'm getting duplicates. Anyways I'm doing it like this and I'm pretty sure I'm doing it wrong:
On the page load i load my dropdownlist to get all the items plus the following to get the saved value:
DropDownListCust.SelectedItem.Text = sql.Customers;
This makes my DDL very buggy, some items dissapear and also sometimes dupicated values. Can anyone please help? I'm using LINQ but I can use some other methods as long as it's fixed.
Cheers
I solved my problem like this:
DropDownList1.ClearSelection();
DropDownList1.Items.FindByText(sql.Customer).Selected = true;

findcontrol does not find dynamically added control which was just addes one line before

who can explain this to me?
CheckBox ckRequest = new CheckBox();
ckRequest.ID = "ckRequest";
ckRequest.DataBinding += new EventHandler(this.CkIsRequested_DataBinding);
container.Controls.Add(ckRequest);
Control con = container.FindControl("ckRequest");
Debugging shows that con is still null.
Debugging also shows me, that conteiner.Controls hase one Item with ID "ckRequest"
How can this be????
Many thanks for your answers.
Actually I try the following.
findcontrol does not find dynamically created control in rowUpdating eventhandler
It makes sense to me, that findcontrol works only on the created page.
At which point in time the visual tree of the page is created?
FindControl only works when the control is in the visual tree of the page
In your case you can try this
var checkBoxesInContainer = container.Controls.OfType<CheckBox>();
http://msdn.microsoft.com/en-us/library/bb360913.aspx
You can use the following:
Control con =
container.Controls.Cast<Control>().First(item => item.ID == "ckRequest");
You might want to try to following:
//GET THE CHECKBOXLIST
Control c = phCategories.Controls.Cast<Control>().First(item => item.ID == "cblCatID-" + catID && item.GetType().Name == "CheckBoxList");
if (c.GetType().Name == "CheckBoxList")
{
cbl = (CheckBoxList)c;
}
For some reason I needed to cast this as a control first. If I did not do it this way, I seemed to grab a label instead (which didn't make sense to me, because it wasn't actually grabbing a label). Hope it helps somebody.

Passing parameter ot RDL report for ASP.NET

I am trying to call rdl reports remotely in ASP.NET, And i was successful calling report without parameter. But when i pass parameter, reporting i not populating and not giving error. It display noting in report. find my code below. and please do suggest me on the same.
MyReportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
MyReportViewer.ServerReport.ReportServerUrl = new Uri(#"http://gblon9sqm10 /ReportServer_DB10");
MyReportViewer.ServerReport.ReportPath = "/Reports/Report1";
MyReportViewer.ShowParameterPrompts = false;
MyReportViewer.ShowPrintButton = true;
ReportParameter[] rptParameters = new ReportParameter[1];
rptParameters[0] = new ReportParameter();
rptParameters[0].Name = "exposureType";
rptParameters[0].Values.Add("Impressions");
MyReportViewer.ServerReport.SetParameters(rptParameters);
MyReportViewer.ServerReport.Refresh();
It's been a while since I set this up but I remember having to make sure that you didn't setup the report again on postback. This is my code in page_load:
if (!Page.IsPostBack)
{
rptViewer.ServerReport.ReportServerUrl = Settings.ReportServerUrl;
if (rptViewer.ServerReport.ReportServerCredentials == null)
rptViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials();
List<ReportParameter> parameters = new List<ReportParameter>();
parameters.Add(new ReportParameter("TitleLabel", "Title string here"));
//More parameters added here...
rptViewer.ServerReport.SetParameters(parameters);
}
I was facing the same problem with same code and configuration as you have mentioned.
I tried bit extra work and get the rid of problem.
I have created a new report with single parameter "Name" and pass value to this parameter from code-behind as ReportParameter. At the report configuration side set the type of parameter text and allow to blank values. Note here i didn't touch any other settings of parameter means kept it as comes by default.
This works for me and then i started adding more parameters and it works perfectly fine.
There is no any browser constraint.
Try as said above and still you face issue then reply me i will put a sample code.
Hope it will help you.

Binding LinqDataSource from code-behind to Gridview

i have a grdidview control on the .aspx page and i am trying to connect dynamically from code behind and bind the gridview but somehow it throwing me an error... what is wrong with this code? any help?
LinqDataSource LDS_POReport = new LinqDataSource();
LDS_POReport.ContextTypeName = "DataContextDataContext";
LDS_POReport.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSourcePO_Selecting);
this.gvReport.DataSource = "LDS_POReport";
//this.gvReport.DataBind();
Update:
after i update the code to
this.gvReport.DataSource = LDS_POReport;
it works fine but when i try to sort i get this error:
The GridView 'gvReport' fired event Sorting
which wasn't handled.
i added this but no effect.
LDS_POReport.AutoPage = true;
LDS_POReport.AutoSort = true;
I guess that your problem is here:
this.gvReport.DataSource = "LDS_POReport";
The above code line attempts to assign a string to a property that expects some sort of data source. I assume that you really intended to assign the LinqDataSource object itself:
this.gvReport.DataSource = LDS_POReport;
First thing, the DataSource should get a reference to the object containing the data, not the name of the object containing the data. GridViews can work reflectively, but not THAT reflectively.

Eval versus DataField in ASP:Datagrid

I have this really random problem with is bugging me. It works at the end of the day but the problem took some time to figure out and was wondering why this happens so if anyone shed some light on the subject I would be really grateful. Here is the problem
I have the following two columns on my datagrid
<asp:boundcolumn
datafield="contentsection.Id"
headerstyle-cssclass="dgHead"
headertext="Section"
itemstyle-cssclass="dgItem" />
and
<asp:templatecolumn>
<itemtemplate><%#Eval("contentsection.Id") %></itemtemplate>
</asp:templatecolumn>
The first column gives me the error of:
A field or property with the name 'contentsection.Id' was not found on the selected data source
but the second on runs fine. Any ideas or theories as to why this is happening ?
The way that I call and bind my data is like so:
Page Load Code Behind
ContentList.DataSource = ContentBlockManager.GetList();
ContentList.DataBind();
The GetList() function it is overloaded and by default passed a 0
public static List<ContentBlockMini> GetList(int SectionId)
{
List<ContentBlockMini> myList = null;
SqlParameter[] parameters = { new SqlParameter("#ContentSectionId", SectionId) };
using (DataTableReader DataReader = DataAccess.GetDataTableReader("dbo.contentblock_get", parameters))
{
if (DataReader.HasRows)
{
myList = new List<ContentBlockMini>();
}
while (DataReader.Read())
{
myList.Add(FillMiniDataRecord(DataReader));
}
}
return myList;
}
and my filling of the data record. The ContentSection is another Object Which is a property of the ContentBlock object
private static ContentBlockMini FillMiniDataRecord(IDataRecord DataRecord)
{
ContentBlockMini contentblock = new ContentBlockMini();
contentblock.Id = DataRecord.GetInt32(DataRecord.GetOrdinal("Id"));
contentblock.Name = DataRecord.GetString(DataRecord.GetOrdinal("Name"));
contentblock.SEOKeywords = DataRecord.IsDBNull(DataRecord.GetOrdinal("SEOKeywords")) ? string.Empty : DataRecord.GetString(DataRecord.GetOrdinal("SEOKeywords"));
contentblock.SEODescription = DataRecord.IsDBNull(DataRecord.GetOrdinal("SEODescription")) ? string.Empty : DataRecord.GetString(DataRecord.GetOrdinal("SEODescription"));
if (DataRecord.GetInt32(DataRecord.GetOrdinal("ContentSectionId")) > 0)
{
ContentSection cs = new ContentSection();
cs.Id = DataRecord.GetInt32(DataRecord.GetOrdinal("ContentSectionId"));
cs.Name = DataRecord.IsDBNull(DataRecord.GetOrdinal("ContentSectionName")) ? string.Empty : DataRecord.GetString(DataRecord.GetOrdinal("ContentSectionName"));
contentblock.contentsection = cs;
}
return contentblock;
}
There you go that is pretty much the start to end.
Databinding can only access "top level" properties on an object. For example, if my object is
Company with simple properties CompanyId and Name and a child object for Address, databinding can only access CompanyId and Name, not Company.Address.City.
Eval can access child object properties if you import the namespace using an #Import page directive.
The "Bind" marker is a two-way bind and is made using the default namespace attached to the datasource of the object. This can't be overridden, and because of this tightly coupled connection the namespace should be left off as assumed (since it's dynamically pulled from the datasource).
Eval is a one-way binding that can be used with any namespace that will fulfill a proper evaluation. Dates, strings, method calls, etc. The namespace provided in your example just provides eval with a marker on where to get the data relevant.
The key here is the nature of the binding. Eval is a "fire and forget" sort of binding where as Bind is more rigid to facilitate the two-way.
The DataField property approach isn't complicated enough to support dot notation; it expects a direct reference in your underlying data source; it takes that field and checks the data source, which it can't find it by the exact string.
The second approach, eval, is more dynamic, but actually I wasn't sure if it supported dot notation... learn something everyday.
HTH.
It's necessary take a look to your DataSource, but you can do a simple test: convert the first column to a template column a try to Run.
In EntityFramework is possible Eval Properties using code like this: <%#Eval("Customer.Address.PostalCode") %>.
I dont know if it is your case, but if you provide more details about how to retrieve the DataSource we can help you.

Resources