Not able to use resources inside .cs file - asp.net

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

Related

Post a file from codebehind

I want to upload a file from codebehind.
I would like to explain my situation a bit.
My existing code is..
I have a fileupload control.
<asp:FileUpload ID="fuSRForm" runat="server" class="file"/>
Then from the codebehind, I check the conditions for that control.
if (fuSRForm.HasFile && (fuSRForm.PostedFile.ContentType.ToString().Trim().ToLower().Contains("pdf") || fuSRForm.PostedFile.ContentType.ToString().Trim().ToLower().Contains("application/vnd.openxmlformats-officedocument.wordprocessingml.document") || fuSRForm.PostedFile.ContentType.ToString().Trim().ToLower().Contains("msword")))
And if the conditions are met,
I used the Sitecore API to upload the file.
This is the part of uploading API.
// creating necessary arguments to be passed to the processor
UploadArgs args = new UploadArgs();
// adding http files collection
args.Files = base.Request.Files;
So, the API is grabbing all posted files by using base.Request.Files
My new situation is that I creat a pdf file when the user click Submit.
Then I save it in a folder named asyncupload
After that I have to upload it to Sitecore using the same API
So, I tried to change the base.Request.Files to my file.
But I am unable to change that.
So, I will have to upload my file using FileUpload control.
FileUpload tempFU = new FileUpload();
tempFU.PostedFile= ????
args.Files = base.Request.Files;
I am stuck right here. I can either post my file from code behind or change the base.Request.Files to my file.
either way, I am stuck. Anyone can solve that?
I'm not sure what your end goal here is. Maybe you could post some more code if I've understood you incorrectly. But here's how to add a file to the Media Library: Brian Pedersen: Adding a file to the Sitecore Media Library programatically
If you are using an <asp:FileUpload /> control, then you can save the file to server disk like this:
string filePath = Path.Combine(Server.MapPath("~/"), "upload/tempfile.tmp");
FileUpload1.SaveAs(filePath);
Then call the AddFile method from the link I provided above:
AddFile(filePath);
You need to use handler in the api you are going to hit and hit it by
using (WebClient client = new WebClient())
{
client.UploadFile(Sitecore Handler, FilePath);
}

Get the data from the "Version" Tab in Dll properties with c# code?

I want to get the FileVersion (or ProductVersion) and the Comments fields which are in the "Version" tab of a dll using a c# code.
I tried to use the Assembly class, but didn't find the data I need.
Thanks in Advance.
That's because the Assembly only knows the AssemblyVersion. To get File information, you need to look at the file properties using FileVersionInfo.

ASP.NET setting a path for file with VB

So this is a pretty dumb question, and one I am clearly misunderstanding for whatever the reason. I have an ASP.net project and the default aspx page loads a file. I made a class to deal with file handling, in it I hard coded the local directory to where that file is. I want to make that path relative to the default.aspx page. I can't figure out how to do that. I have read a lot of stuff on MSDN, and it makes simple sense, but when I put it to code I can't seem to get it right.
I feel my answer is here, I understand what it is saying but I can't translate that to my program.http://msdn.microsoft.com/en-us/library/ms178116.aspx. Code such as
Dim rootPath As String = Server.MapPath("~")
makes sense to me but I can't use 'Server' for some reason.
I have used this spot of code to supplement my problem for now.
string=System.AppDomain.CurrentDomain.BaseDirectory()
Any help leading me on the right....path, would be appreciated.
Try
Dim rootPath As String = HttpContext.Current.Server.MapPath("~")
from your class.

Newbie question: Need help with ContextTypeName

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.

Access compilation element in web.config [duplicate]

Is there any way to access the <compilation /> tag in a web.config file?
I want to check if the "debug" attribute is set to "true" in the file, but I can't seem to figure out how to do it. I've tried using the WebConfigurationManager, but that doesn't seem to allow me to get to the <compilation /> section.
Update:
I know I could easily just load the file like an XML Document and use XPath, but I was hoping there was already something in the framework that would do this for me. It seems like there would be something since there are already ways to get App Settings and Connection Strings.
I've also tried using the WebConfigurationManager.GetSection() method in a few ways:
WebConfigurationManager.GetSection("compilation")// Name of the tag in the file
WebConfigurationManager.GetSection("CompilationSection") // Name of the class that I'd expect would be returned by this method
WebConfigurationManager.GetSection("system.web") // Parent tag of the 'compilation' tag
All of the above methods return null. I'm assuming there is a way to get to this configuration section since there is a class that already exists ('CompilationSection'), I just can't figure out how to get it.
Use:
using System.Configuration;
using System.Web.Configuration;
...
CompilationSection configSection =
(CompilationSection) ConfigurationManager.GetSection( "system.web/compilation" );
You can then check the configSection.Debug property.
TIP: if you need to know how to get a value from a config file, check the machine.config file in your \Windows\Microsoft.net\Framework\<version>\CONFIG folder. In there you can see how all the configuration section handlers are defined. Once you know the name of the config handler (in this case, CompilationSection), you can look it up in the .Net docs.
The easy way to check if you're running in debug mode is to use the HttpContext.IsDebuggingEnabled property. It gets its answer from the compilation element's debug attribute which is the same thing you're trying to do.
After all, you can always load up the Web.config file into an XmlDocument and use an XPath query to find out!
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Web.config"));
doc.SelectSingleNode("/configuration/system.web/compilation/#debug");
However, I suggest you use the Configuration.GetSection method for solution.
CompilationSection section =
Configuration.GetSection("system.web/compilation") as CompilationSection;
bool debug = section != null && section.Debug;
You can always check debug option on the compiler level by enclosing your code with
#if (DEBUG)
#endif
in case that was the idea...
Try using:
HttpContext.Current.IsDebuggingEnabled
Cant you just load the file up as a regular XML File and use XPath to get the nodes?
Try using the ConfigurationManager.GetSection method.

Resources