c#-2.0

How to convert Dictionary<string, object> to Dictionary<string, string> in c#

与世无争的帅哥 提交于 2019-12-28 11:42:51
问题 I have below code in C# Dictionary<string, object> dObject = new Dictionary<string, object>(); I want to convert dObject to Dictionary<string, string> . How can I do this? 回答1: Use the ToDictionary method: Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value.ToString()); Here you reuse the key from the original dictionary and you convert the values to strings using the ToString method. If your dictionary can contain null values you should add a null check before

C# Xml Serialization & Deserialization

不问归期 提交于 2019-12-28 04:08:30
问题 I am trying to serialize an object & save it into a Sql server 2008 xml field. I also have some deserialization code that re-hydrates the object. I am able to serialize & save the object into the db, but get a "Root element missing" exception. [XmlRoot("Patient")] public class PatientXml { private AddressXml _address = null; private EmergencyContactXml _emergencyContact = null; private PersonalXml _personal = null; [XmlElement] public PersonalXml Personal { get { return _personal; } set {

Save and reload app.config(applicationSettings) at runtime

一世执手 提交于 2019-12-28 03:01:06
问题 I've stored configuration of my application in the app.config, by Visual Studio I've created some application key on the settings tab of project properties dialog, then I've set this key at application level(NOT at user level). Visual Studio automatically generate the following xml file (app.config) : <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,

C# - Why can I not cast a List<MyObject> to a class that inherits from List<MyObject>?

有些话、适合烂在心里 提交于 2019-12-25 04:33:30
问题 I've got an object, which I'll call MyObject. It's a class that controls a particular data row. I've then got a collection class, called MyObjectCollection: public class MyObjectCollection : List<MyObject> {} Why can I not do the following: List<MyObject> list = this.DoSomethingHere(); MyObjectCollection collection = (MyObjectCollection)list; Thanks in advance. Edit: The error is InvalidCastException 回答1: My guess is that DoSomethingHere doesn't return an instance of MyObjectCollection . Let

Questions about Window Handles

走远了吗. 提交于 2019-12-25 04:29:25
问题 lang: c#.net 2.0 I have a Application X, that calls SubWindows 1, 2 and 3. I will Pop up a MessageBox, when 1, 2 or 3 is opened. My idea is a timer that ticks every 3 seconds and checks if the windows are opened. But how to find the 3 exact windows? Can I use a Window-Handle-Spy-Application and Hardcode the Window-Handles in my code to find them ever? Or change the window handle when the Application X is opened new? 回答1: I'm going to answer in terms of Win32 because I'm more familiar with

ASP.Net ViewState doesn't work when Control become Enable=False

China☆狼群 提交于 2019-12-25 02:53:53
问题 Why when a control does contain value but it's set to .Enable=False that all controls become disable (that's ok) but why that the ViewState doesn't retain the data on the next post back? If I get the UserControl without modifing its Enable state, the ViewState work between post back. How can we disable a UserControl that all its control become disable (this part work) but all of them KEEP use the ViewState (this doesn't work)? Clarification: 1)In the aspx.cs click button EDIT: myControl

Replace tokens in an aspx page on load

瘦欲@ 提交于 2019-12-25 02:38:49
问题 I have an aspx page that contains regular html, some uicomponents, and multiple tokens of the form {tokenname} . When the page loads, I want to parse the page content and replace these tokens with the correct content. The idea is that there will be multiple template pages using the same codebehind. I've no trouble parsing the string data itself, (see named string formatting, replace tokens in template) my trouble lies in when to read, and how to write the data back to the page... What's the

Deserialize XML

倾然丶 夕夏残阳落幕 提交于 2019-12-25 02:26:09
问题 I want to deserialize a XML file in C# (.net 2.0). The structure of the XML is like this: <elements> <element> <id> 123 </id> <Files> <File id="887" description="Hello World!" type="PDF"> FilenameHelloWorld.pdf </File> </Files> </element> <elements> When I try to deserialize this structure in C#, I get a problem with the Filename, the value is always NULL, even how I try to code my File class. Please help me. ;-) 回答1: The following works fine for me: public class element { [XmlElement("id")]

Preserving format while passing DateTime values to Stored Procedure

£可爱£侵袭症+ 提交于 2019-12-25 02:08:28
问题 I have a TextBox server control on a Web Form, say, txtDueDate . I don't want to use DateTimePicker but want to use TextBox itself. The user enters date in dd/mm/yyyy format. While passing this value to SQL Server 2005 (Express) stored procedure, I use something like: cmdParameters.AddWithValue("@DueDate", Convert.ToDateTime(txtDueDate.Text)); The stored procedure has input parameter to handle this value and is declared as: @DueDate SmallDateTime I want to know the default format SQL Server

Cannot Transfer request to Desired ASP.NET Error Page

扶醉桌前 提交于 2019-12-24 18:58:15
问题 In the Page_Load() section, I check for valid inputs & incase they are invalid, I transfer the request to a custom error page. While doing so, a ThreadAbortException is thrown which is caught by my catch block but asp.net transfers the request to unknown exception page. What am I doing wrong? I dont want the ThreadAbortException to come when I transfer to the error page. eg: protected void Page_Load(object sender, EventArgs e) { try { if (String.IsNullOrEmpty(szProductName)) { //Product name