Newbie question: Need help with ContextTypeName - asp.net

I was following the Contoso University tutorial on the asp.net website and it all works well. My next step was to replicate some of the things on my own new website. I have added an EntityDataSource that works:
<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server"
ConnectionString="name=MyWebsite2011Entities"
DefaultContainerName="MyWebsite2011Entities" EnableFlattening="False"
EntitySetName="product_type">
And according to the tutorial it is a good idea to replace the ConnectionString and DefaultContainerName with a ContextTypeName.
"In the markup for the EntityDataSource control, remove the ConnectionString and DefaultContainerName attributes and replace them with a ContextTypeName="ContosoUniversity.DAL.SchoolEntities" attribute. This is a change you should make every time you create an EntityDataSource control, unless you need to use a connection that is different from the one that's hard-coded in the object context class."
That worked fine for me in the tutorial where they had a:
<asp:EntityDataSource ID="StudentsEntityDataSource" runat="server"
ContextTypeName="ContosoUniversity.DAL.SchoolEntities"
EnableFlattening="False"
EntitySetName="People"
EnableDelete="True" EnableUpdate="True">
</asp:EntityDataSource>
The difference for me (besides the project name) is that my entity model is not placed in a DAL folder. Instead I accepted the Visual Web Developer's default folder name recommendation. I believe it was "App_Code". But the ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities" doesn't work. When I start the browser, it complains that the type MyWebsite2011.App_Code.MyWebsite2011Entities could not be read.
<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server"
ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities"
EnableFlattening="False" EntitySetName="product_type">
How do I find the correct ContextTypeName to put in? As I said, the ConnectionString and DefaultContainerName worked, so I guess the MyWebsite2011Entities is ok. Any hints would be appreciated because I don't know the naming convention or what to look for.

Open up the .cs file in which the Context is declared, and look at text immediately after the namespace declaration. That's your class's namespace. Your ContextTypeName should be <namespace>.<classname> (without the <> brackets, of course.)

I was following the same Contoso University tutorial but I was using windows Azure as an online Database.
So I also ran into this problem.
For other people interested on how to fix this when using an online database, I did the following.
With your EntityDataSource do:
Remove the following:
ConnectionString=...
DefaultContainerName=...
Add:
OnContextCreating="EntityDataSource_ContextCreating"
Leaving ContextTypeName empty.
And finally implement the following code in the corresponding .cs:
protected void EntityDataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
<ContainerName> context = new <ContainerName>();
e.Context = ((IObjectContextAdapter)context).ObjectContext;
}
ContainerName being your containername ofcourse.
If we would look at the original question then it would be MyWebsite2011Entities.

Related

How to use session.contents as a variable in ASP SQLDataSource Visual Studio 2017

I am using Visual Studio 2017 to build an ASP.NET website (using VB) and have set up a few different connection strings in my web config file for different user types (i.e. basic access, admin access, etc.). Each connection type has access to different rights as well as different schemas in the DB. I'm trying to call these different connections via a session contents variable that is generated at the time of a successful login (on the login page) and then used in other pages on the site.
This is an example of the ASP connection string used on one of the other pages:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:DBSource1 %>" SelectCommand="Stored_Proc"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Here's the part where I would like to call via session contents to use as a variable in the SQLDataSource:
ConnectionString="<%$ ConnectionStrings:DBSource1 %>"
Here's the Session content code I'd like to use as a variable:
Session("SqlConstrName").ToString()
I am able to put the session content into a text box in the ASP code and it gives the connection name as it's shown above
ConnectionStrings:DBSource1
I have done some research and know that I cannot use <%= % for server side controls and have been unsuccessful using <%# %, "<%# %", or any other combination type to pass the variable through. I have also tried to bind just the name part and exclude the "ConnectionStrings:" part, but was still unsuccessful.
I know that I can easily create a connection string in my VB code and then bind the data to the objects, but I am attempting to do this on the ASPX side so that my drop-downs, grid-views, etc. stay bound which lowers the amount of VB code needed on the backend and allows me to have more dynamic tables that link between other items on the page without having to write a bunch of on change events. Any suggestions or thoughts is greatly appreciated, thanks!
If your scenario is exactly the same as the drag'n'drop control designer envisaged then you should have no problems.
If you need any deviation from what they created then you are probably better off writing the code yourself.
Either way, there is code somewhere - the difference is between you controlling the code you wrote or you fighting against the code someone else wrote.
IMHO.

Not able to use resources inside .cs file

I have project asp.net with namespace test and I'm using resources (files Resource.resx and Resource.en-GB.resx). I have in resources key MY_TEXT with value. In file .aspx I can use
<%# Resources.Resource.MY_TEXT %>
but when I'm using (like in article http://msdn.microsoft.com/en-us/magazine/cc163566.aspx)
myLabel.Text = Resource.MYTEXT;
It don't found it and I have build error. Why?
Best regards,
Dagna
Try something like this:
myLabel.Text = Resources.Resource.MYTEXT;
Let me know if it works or what kind of error the compiler gives you.
You can also write it as you wrote in your question, click on Resources to put cursor there, hit Ctrl + . and select a namespace to be added. This should make Visual Studio detect a valid Resource namespace (or suggest few available namespaces) automatically.
Update
This article explains how to use resources in code behind:
http://msdn.microsoft.com/en-us/library/ms227982%28v=vs.85%29.aspx. I mentioned it in comments and it helped you, so I'm adding it as a part of an answer.
You can get a text from resources using some methods in aspx.cs file.
For local resources:
myLabel.Text = GetLocalResourceObject("MYTEXT").ToString();
For global resources:
myLabel.Text = GetGlobalResourceObject("MYTEXT").ToString();
For more information visit: https://msdn.microsoft.com/en-us/library/ms227982%28v=vs.140%29.aspx

ASP.NET SqlDataSource bulk update

MSDN Library has a walkthrough for bulk updates using a SqlDataSource.
http://msdn.microsoft.com/en-us/library/aa992036(v=VS.90).aspx
Not sure what I'm doing wrong,
but I keep getting an error at:
currentID = Convert.ToInt32(GridDocuments.DataKeys(0).Value)
Are there other resources for SqlDataSource bulk updates?
The line in the example reads:
currentID = Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value);
This line assumes that you are setting datakeys on your grid, pay special attention to the DataKeyNames attribute in the example: DataKeyNames="EmployeeID"
This example is a little easier to follow. If you would like help debugging the other example please post the error your getting as well.

ASP.NET: two ways to access global resource programmatically

I know that I can set a Label's text by using the following syntax.
lblMessage.Text = (string)GetGlobalResourceObject("resxFile", "message");
What are the benefits and drawbacks associated with using the below syntax?
lblMessage.Text = Resources.resxFile.message;
The second method will not work for local resource files. Is there a different syntax for local resource files?
The second way looks better because it is strongly-typed. If you changed the resource file name or the resource value name then you would get a compile error. If you needed to dynamically get a resource, then you would have to do it the first way, else use a switch statement or something similar.
If you are using asp.net 2.0 or higher there is actually a 3rd way to set a label by using markup only:
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:resxFile,message %>" />
Kinda related to localization: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/localization.aspx

What is going on in this ExecuteDataset method?

OK so I one page I find this line:
objDsCourse = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings("connstr").ConnectionString, CommandType.StoredProcedure, "Course_NewReportGet_Get_Sav", objPAra)
And I copied it to another page to start modifying it to work there:
getData = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings("connstr").ConnectionString, CommandType.StoredProcedure, "Course_NewReportGet_Get_Sav", objPAra)
However on the new page it underlines .ConnectionStrings saying that Non-invocable member 'System.Configuration.ConfigurationManager.ConnectionStrings' cannot be used like a method'... then why did it work in the other page??
EDIT: OK so I found in web.config what I think it is referencing because it says
<add name="ConnStr" connectionString="data source=..." />
Why would one page have access to this and the other not?
Is there any chance one page is using VB.NET, while the other is using C#?
I would agree with Daniel. In Visual Basic, both dictionary objects and methods are referenced by using parentheses. This can cause some confusion.
So in VB, ConfigurationManager.ConnectionStrings("connstr") would point to the ConnectionString object with the key "connstr" in the dictionary.
In C#, dictionary objects are referenced by square brackets [] so ConfigurationManager.ConnectionStrings("connstr") would literally mean "invoke the method ConnectionStrings of ConfigurationManager object using "connstr" as a parameter."
Long story short, check the <%# Page %> declaration at the top to make sure both pages are the same language. ... or, on the page with the error, change the line to use the ConfigurationManager.ConnectionStrings["connstr"] syntax.

Resources