|
Dynamically Created Controls in ASP.NET[^]
You're probably not recreating the controls when the page posts back; or you're not recreating them early enough.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
public void ResetPassword(UserModel model, string Name)
{
using (var db = new sortit_internEntities())
{
string emailAddress = (from info in db.users
where info.name.Equals(model.Name)
select info.email).Single();
if (!string.IsNullOrEmpty(emailAddress))
{
string confirmationToken =
WebSecurity.GeneratePasswordResetToken(model.Name);
dynamic email = new Email("ChngPasswordEmail");
email.To = emailAddress;
email.UserName = model.Name;
email.ConfirmationToken = confirmationToken;
email.Send();
}
}
}
for resetting password for login and yes I am new to MVC, this code is in business layer of MVC for kind Information
|
|
|
|
|
Put the appropriate Using statement at the top of the class module and make sure you have included a reference to the appropriate assembly
|
|
|
|
|
Ya I had add all references for it!
|
|
|
|
|
Look at the error message:
The type or Namespace name "Email" could not be found
It's pretty explicit!
The compiler does not know what the Email class is: it hasn't got one for you to use. Either you haven't added a reference to the assembly containing the class to your project, and a using statement at the top of the file to the namespace the class is in; or just the using statement is missing; or you do not have a class called Email at all.
Hover the mouse over the word "Email" and a little blue bar will appear at the beginning. Hover the mouse over that and a drop down will open suggesting ways to fix it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Nobody else caught this, but why are you using "dynamic"? It's not for the use case what you're using it. That line should read:
Email email = new Email("ChngPasswordEmail");
Dynamic treats your object as an Object type, removes all static checks at compile time, and slows down the code a bit due to having to resolve method calls at runtime. It also makes it harder to write the code and debug it. It should be rare that you have to use the dynamic type.
|
|
|
|
|
I need some help getting this to work. I am trying to read from an XML file that is called "user.config" and is located in "C:\Windows\SysWOW64\config\systemprofile\AppData\Local\MyAppName\user.config". each of the values are Base64 Encrypted (which is nested with more xml). My goal is to be able to read each value independently and store as a variable for later use and to append into textbox1 so that a summary of the settings can be displayed.
Sample XML:
="1.0"="utf-8"
<configuration>
<userSettings>
<AppCentral.App.Service.Core.Properties.Settings>
<setting name="AppXml" serializeAs="String">
<value>PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8QXBwTW9kZWwgeG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYSI+DQogIDxDR1VJRD4xMjM0NTY3ODk8L0NHVUlEPg0KICA8Q0lEPjEyMzwvQ0lEPg0KICA8Q051bWJlcj4xMjM0PC9DTnVtYmVyPg0KICA8Q05hbWU+VEVTVDwvQ05hbWU+DQogIDxIYXNDSEluc3RhbGw+ZmFsc2U8L0hhc0NISW5zdGFsbD4NCiAgPFZlbmRvcklEPjA8L1ZlbmRvcklEPg0KPC9BcHBNb2RlbD4=</value>
</setting>
<setting name="TXml" serializeAs="String">
<value>PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8QXBwVE1vZGVsIHhtbG5zOnhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2UiIHhtbG5zOnhzZD0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEiPg0KICA8Q0dVSUQgeHNpOm5pbD0idHJ1ZSIgLz4NCiAgPENJRD4xMjM8L0NJRD4NCiAgPElzTWFuYWdlZFRBbGxvd2VkPmZhbHNlPC9Jc01hbmFnZWRUQWxsb3dlZD4NCiAgPElzT25lVGltZVRFbmFibGVkPmZhbHNlPC9Jc09uZVRpbWVURW5hYmxlZD4NCiAgPE9uZVRpbWVUQ2hhcmdlPjIuMDAwMDwvT25lVGltZVRDaGFyZ2U+DQogIDxXYWl0aW5nUGVyaW9kSG91cnM+MzwvV2FpdGluZ1BlcmlvZEhvdXJzPg0KICA8SXNUVGF4YWJsZT5mYWxzZTwvSXNUVGF4YWJsZT4NCiAgPFRUYXhSYXRlPjQuMDAwMDwvVFRheFJhdGU+DQo8L0FwcFRNb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ICAgIDxDRFNjaGVkdWxlSUQ+MDwvQ0RTY2hlZHVsZUlEPg0KICAgICAgICA8SG91clN0YXJ0PjY8L0hvdXJTdGFydD4NCiAgICAgICAgPEhvdXJFbmQ+NzwvSG91ckVuZD4NCiAgICAgICAgPElzQXZhaWxhYmxlPnRydWU8L0lzQXZhaWxhYmxlPg0KICAgICAgPC9EU2NoZWR1bGU+DQogICAgPC9TY2hlZHVsZXM+DQogIDwvRE1vZGVsPg0KPC9BcnJheU9mRE1vZGVsPg==</value>
</setting>
<setting name="CurrentVersion" serializeAs="String">
<value>1</value>
</setting>
</AppCentral.App.Service.Core.Properties.Settings>
</userSettings>
</configuration>
|
|
|
|
|
|
Deserialize the file into a class (paste special xml as class) if you don't want to parse (e.g. XElement):
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "", IsNullable = false )]
public partial class configuration {
<pre>
private configurationUserSettings userSettingsField;
public configurationUserSettings userSettings {
get {
return this.userSettingsField;
}
set {
this.userSettingsField = value;
}
}
}
/// <remarks>
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class configurationUserSettings {
private configurationUserSettingsSetting[] appCentralAppServiceCorePropertiesSettingsField;
[System.Xml.Serialization.XmlArrayAttribute( "AppCentral.App.Service.Core.Properties.Settings" )]
[System.Xml.Serialization.XmlArrayItemAttribute( "setting", IsNullable = false )]
public configurationUserSettingsSetting[] AppCentralAppServiceCorePropertiesSettings {
get {
return this.appCentralAppServiceCorePropertiesSettingsField;
}
set {
this.appCentralAppServiceCorePropertiesSettingsField = value;
}
}
}
/// <remarks>
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class configurationUserSettingsSetting {
private string valueField;
private string nameField;
private string serializeAsField;
/// <remarks>
public string value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
/// <remarks>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string serializeAs {
get {
return this.serializeAsField;
}
set {
this.serializeAsField = value;
}
}
}
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Thank you. I will give this a try. Is the top one the method and the other 2 below it classes i need to create? Once i am able to read the <value> nodes from the XML it should be much easier after that to decode the base64 and output.
|
|
|
|
|
"configuration" is the "root"; the others are members.
It's what "Visual Studio" interprets from the XML using paste special.
You should study and understand it; then use it.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
last question. what would the method call look like? can i still use "XDocument doc = XDocument.load(FullFilePath)" to load the file?
<pre lang="c#">public string XMLStrings()
{
XDocument doc = XDocument.Load(@"C:\user.config");
}
</pre>
|
|
|
|
|
|
Can you give me an example? im attempting to use it in the same context as that link you provided but that isn't working.
|
|
|
|
|
All that's left is for me "to do it for you".
Not happening.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
sorry, im not looking for you to write it for me. This code is for my own person application i am developing as i learn on my own. I am trying to self teach myself C#. I am not working on this for a school or business project.
I am reading over the class and trying to figure out how to include this in my method. Here is what i have so far. Can you at least show me where i might be going wrong? im trying to figure out how to use the variable i to link it to the class and grab the data. I haven't experimented much with custom built classes yet. i used the link you sent me as a template.
public void XMLStrings(string filename)
{
XmlSerializer serializer = new
XmlSerializer(typeof(XMLClass));
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
XMLClass i;
i = (XMLClass)serializer.Deserialize(reader);
fs.Close();
textBox1.AppendText(
i.something1 + "\t" +
i.something2 + "\t" +
i.something3);
}
|
|
|
|
|
What is the error that you are getting with this code? Couple things that I see are I would wrap the FileStream in a using statement, there are often problems that arise if the stream hasn't been disposed properly. I would also initialize XMLClass. Also, personally I would use a StreamReader not a FileStream if you are trying to just read from the document. So you might try something like:
XMLClass i = new XMLClass();
using(StreamReader stream = new StreamReader(filename))
i = (XMLClass)serializer.Deserialize(stream);
The using statement will open, close, and dispose of you stream properly and if you want to go a step further you could wrap it in a try/catch, and it would catch the error and help you debug exactly what is going wrong.
|
|
|
|
|
that sounds good. I have made that modification.
Currently here is what i have for the methods;
My question from this point is how would i say what <value> node i would like to retrieve from the xml and where it returns that data.
Im looking to input the file name and be able to retrieve each of the individual <value> node values.
public void XMLStrings(string filename)
{
XmlSerializer serializer = new
XmlSerializer(typeof(XMLClass));
XMLClass i = new XMLClass();
using (StreamReader stream = new StreamReader(filename))
i = (XMLClass)serializer.Deserialize(stream);
}
<pre>private void loadXMLData_Click(object sender, EventArgs e) =>
XMLStrings(@"C:\User.config");
|
|
|
|
|
Well your XML class should mirror what your config file tags are. The deserializer is taking a xml file and loading your class object with it. It is matching tags with properties that you set in your XMLClass. For example if your class looked like:
public class XMLClass
{
[System.Xml.Serialization.XmlElement("ID")]
public int ID { get; set; }
[System.Xml.Serialization.XmlElement("Name")]
public string Name { get; set; }
}
Then your XML looks like:
<XMLClass>
<ID> 1 </ID>
<Name> Bob </Name>
</XMLClass>
If you are trying to take a pre-built XML file and load it by node, then I suggest you use the XMLDocument class.
How to read from XML Document
If you are trying to read the config file, I suggest using the ConfigurationManager class.
If you are serializing/deserializing custom classes then use the method I show above.
modified 12-Feb-18 16:50pm.
|
|
|
|
|
sounds like i might have been going the wrong direction then. the XML file is pre-built and external to this application. i am only trying to read it and output its values into a text box. when you say use the XMLDocument Class, are you referring to XDocument and XElement? i was trying to use those prior to posting on here but ran into issues with it returning null values.
|
|
|
|
|
Well that link I put in for the How to read from an XML file shows you how to use it, but without knowing about the document, I can't really help with null values. I mean
XMLDocument doc = new XMLDocument();
doc.LoadXML(filename);
foreach(XmlNode node in doc.DocumentElement.ChildNodes){
string text = node.InnerText;
}
its pretty straight forward, but again if you wrap it in a try/catch and read what the catch says I feel confident the solution will present itself.
|
|
|
|
|
this is working really well and displays the results in a message box. The only issue is that when the message boxes are done being displayed the application seems to freeze up and then finally errors out. i think it is because the loop is still running.
One question... is there a way to change the code i have below to include the name= tag in the filter? If i can do that i might not need the loop and can call the nodes directly. example...;
xmlvalue = node.SelectSingleNode("/setting name="AppXml"/value").InnerText;
private void LoadXMLData_Click(object sender, EventArgs e)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load("C:\\user.config");
XmlNodeList nodes = xdoc.DocumentElement.SelectNodes("/configuration/userSettings/nextnode/setting");
string xmlvalue = "";
foreach (XmlNode node in nodes)
{
if (xmlvalue != null)
{
xmlvalue = node.SelectSingleNode("value").InnerText;
string xmlvalue2 = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(xmlvalue));
MessageBox.Show(xmlvalue2);
}
}
}
modified 12-Feb-18 19:05pm.
|
|
|
|
|
![Go to Parent](https://www.codeproject.com/App_Themes/CodeProject/Img/arrow-up24.png) Sample XML
<configuration>
<userSettings>
<AppCentral.App.Service.Core.Properties.Settings>
<setting name="AppXml" serializeAs="String">
<value>PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8QXBwTW9kZWwgeG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYSI+DQogIDxDR1VJRD4xMjM0NTY3ODk8L0NHVUlEPg0KICA8Q0lEPjEyMzwvQ0lEPg0KICA8Q051bWJlcj4xMjM0PC9DTnVtYmVyPg0KICA8Q05hbWU+VEVTVDwvQ05hbWU+DQogIDxIYXNDSEluc3RhbGw+ZmFsc2U8L0hhc0NISW5zdGFsbD4NCiAgPFZlbmRvcklEPjA8L1ZlbmRvcklEPg0KPC9BcHBNb2RlbD4=</value>
</setting>
<setting name="TXml" serializeAs="String">
<value>PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8QXBwVE1vZGVsIHhtbG5zOnhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEt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GU+ODwvVHlwZT4NCiAgICA8Q1BvcnQ+NTwvQ1BvcnQ+DQogICAgPFJJbmRleD4xPC9SSW5kZXg+DQogICAgPElzQWN0aXZlPmZhbHNlPC9Jc0FjdGl2ZT4NCiAgICA8T3BhY2l0eT4wPC9PcGFjaXR5Pg0KICAgIDxTY2hlZHVsZXMgLz4NCiAgPC9ETW9kZWw+DQogIDxETW9kZWw+DQogICAgPENESUQ+MzwvQ0RJRD4NCiAgICA8TmFtZT5EMjwvTmFtZT4NCiAgICA8VHlwZT4xPC9UeXBlPg0KICAgIDxDUG9ydD4zPC9DUG9ydD4NCiAgICA8UkluZGV4PjE8L1JJbmRleD4NCiAgICA8SXNBY3RpdmU+ZmFsc2U8L0lzQWN0aXZlPg0KICAgIDxPcGFjaXR5PjA8L09wYWNpdHk+DQogICAgPFNjaGVkdWxlcz4NCiAgICAgIDxEU2NoZWR1bGU+DQogICAgICAgIDxDRFNjaGVkdWxlSUQ+MDwvQ0RTY2hlZHVsZUlEPg0KICAgICAgICA8SG91clN0YXJ0PjY8L0hvdXJTdGFydD4NCiAgICAgICAgPEhvdXJFbmQ+NzwvSG91ckVuZD4NCiAgICAgICAgPElzQXZhaWxhYmxlPnRydWU8L0lzQXZhaWxhYmxlPg0KICAgICAgPC9EU2NoZWR1bGU+DQogICAgPC9TY2hlZHVsZXM+DQogIDwvRE1vZGVsPg0KPC9BcnJheU9mRE1vZGVsPg==</value>
</setting>
<setting name="CurrentVersion" serializeAs="String">
<value>1</value>
</setting>
</AppCentral.App.Service.Core.Properties.Settings>
</userSettings>
</configuration>
|
|
|
|
|
sorry, im not looking for you to write it for me. This code is for my own person application i am developing as i learn on my own. I am trying to self teach myself C#. I am not working on this for a school or business project.
I am reading over the class and trying to figure out how to include this in my method. Here is what i have so far. Can you at least show me where i might be going wrong? i used the link you sent me as a template.
<pre lang="c#"> public void XMLStrings(string filename)
{
// Create an instance of the XmlSerializer specifying type and namespace.
XmlSerializer serializer = new
XmlSerializer(typeof(XMLClass));
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
// Declare an object variable of the type to be deserialized.
XMLClass i;
// Use the Deserialize method to restore the object's state.
i = (XMLClass)serializer.Deserialize(reader);
fs.Close();
// Write out the properties of the object.
textBox1.AppendText(
i.something1 + "\t" +
i.something2 + "\t" +
i.something3);
}</pre>
|
|
|
|
|
Nobody seems to have mentioned this, but Base64 is NOT an encryption scheme. It's one of a bunch of encoding schemes originally designed to enable the transport of binary data over links that doesn't support it.
Look familiar?
<?xml version="1.0"?>
<AppModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CGUID>123456789</CGUID>
<CID>123</CID>
<CNumber>1234</CNumber>
<CName>TEST</CName>
<HasCHInstall>false</HasCHInstall>
<VendorID>0</VendorID>
</AppModel>
Why are you embedding XML inside an XML file? This seems a bit overly complicated.
|
|
|
|