c#-2.0

How to get application path

给你一囗甜甜゛ 提交于 2019-11-29 10:51:32
i am using string path = AppDomain.CurrentDomain.BaseDirectory; to get my application path ,but this gives something like C:\Projects\XYZ\ABC\bin\Debug i don't want bin\Debug .Is there any way to achieve this ? The AppDomain.CurrentDomain.BaseDirectory property gets the base directory that the assembly resolver uses to probe for assemblies. So it's functioning 100% as it should. If you were to build your application, cut and paste it somewhere else in another folder or drive. Those changes would be reflected in this property. Also, you mentioned that you do not want this part bin\Debug , so

Get domain name of a url in C# / .NET [duplicate]

大憨熊 提交于 2019-11-29 09:22:52
This question already has an answer here: Top level domain from URL in C# 8 answers The code: string sURL = "http://subdomain.website.com/index.htm"; MessageBox.Show(new System.Uri(sURL).Host); gives me "subdomain.website.com" But I need the main domain "website.com" for any url or web link. How do I do that? You can do this to get just the last two segments of the host name: string[] hostParts = new System.Uri(sURL).Host.Split('.'); string domain = String.Join(".", hostParts.Skip(Math.Max(0, hostParts.Length - 2)).Take(2)); Or this: var host = new System.Uri(sURL).Host; var domain = host

Exception during iteration on collection and remove items from that collection [duplicate]

怎甘沉沦 提交于 2019-11-29 07:27:26
This question already has an answer here: What is the best way to modify a list in a 'foreach' loop? 11 answers I remove item from ArrayList in foreach loop and get follwing exception. Collection was modified; enumeration operation may not execute. How can I remove items in foreach, EDIT: There might be one item to remove or two or all. Following is my code: /* * Need to remove all items from 'attachementsFielPath' which does not exist in names array. */ try { string attachmentFileNames = txtAttachment.Text.Trim(); // Textbox having file names. string[] names = attachmentFileNames.Split(new

How to make a textbox accept only digits and formats numbers with commas?

江枫思渺然 提交于 2019-11-29 07:20:36
I need a text box that: (1) only accepts digits as characters. (2) automatically continues to format numeric values with commas as the user types into it. For example, 1 becomes 1.00 10 becomes 10.00 100 becomes 100.00 1000 becomes 1,000.00 10000 becomes 10,000.00 100000 becomes 1,00,000.00 How to achieve that? Formatting a number while the user is typing in general works very poorly. You should use a MaskedTextBox for that. Plenty of code about on the Internet that shows how to filter KeyPress so only digits can be entered. Most of it is trivially defeated by using the Paste command. The sane

How to insert null into database?

眉间皱痕 提交于 2019-11-29 07:09:36
Hi I am trying to insert null in a database column depending on a gridview datakeys value (if being "" insert null into database) However, I am getting a space ' ' inside the database column. string sbcId = gvTest.DataKeys[gr.RowIndex]["myColumn"].ToString(); insgnp.Parameters.Add(new OleDbParameter("EMPID", (sbcId==""?DBNull.Value.ToString():sbcId))); You have to rewrite your code: if(string.IsNullOrEmpty(sbcId)) Parameters.Add(new OleDbParameter("EMPID", DBNull.Value)); else Parameters.Add(new OleDbParameter("EMPID", sbcId)); The problem with the ternary if statement that you have is that

How to load local HTML pages in WebBrowser control in C#

守給你的承諾、 提交于 2019-11-29 07:00:49
I have number of local HTML pages. I want to display these local HTML pages in Web browser control. When I add new page it should get append to previous page. Here is the sample code for the setting Url for( int i=0; i<=filecount; i++) web-browser.Url = new Uri(filepath[i]); But during run time its showing the File Download pop up and web browser is empty. You could load a single page as FileStream source = new FileStream(filepath, FileMode.Open, FileAccess.Read); webBrowser1.DocumentStream = source; or even like string html = File.ReadAllText(filepath); webBrowser1.DocumentText = html; But if

Server.Transfer throws Error executing child request. How to resolve?

拥有回忆 提交于 2019-11-29 05:26:36
I have a HttpModule in C# 2.0 which handles exceptions thrown. Whenever the exception is thrown, an error page (aspx) with some querystring will be called. It is done through Server.Transfer() . But when the control tries to execute Server.Transfer() , the following exception is thrown: Error executing child request for [pagename].aspx. Whereas Request.Redirect() works fine. I tried setting EnableViewStateMac="false" in Page directive of the page to which request is transferred. Still problem persists. Here is the code I tried: string errorPage = "errorpage.aspx?id=" + someErrorId HttpContext

DateTime string parsing

社会主义新天地 提交于 2019-11-29 04:03:43
I have made a generic parser for parsing ascii files. When I want to parse dates, I use ParseExact function in DateTime object to parse, but I get problems with the year. The text to parse is i.e. "090812" with the parseExact string "yyMMdd". I'm hoping to get a DateTime object saying "12/8-2009", but I get "12/8-1909". I know, that I could make an ugly solution by parsing it afterwards, and thereby modifying the year.. Anyone know of a smart way to solve this ?? Thanks in advance.. Søren Theoretically elegant way of doing this: change the TwoDigitYearMax property of the Calendar used by the

Using Reflection to set a static variable value before object's initialization?

不打扰是莪最后的温柔 提交于 2019-11-29 03:01:52
Is there anyway to set the value of a static (private) variable on an object that has not been initialized? The SetValue method requires an instance, but I'm hoping there's a way to get around this. JaredPar For static values you can pass null for the instance parameter. var type = typeof(SomeClass); var field = type.GetField("SomeField", BindingFlags.NonPublic | BindingFlags.Static); field.SetValue(null, 42); could you create a static function that is public and use it to set your private static variable ? 来源: https://stackoverflow.com/questions/2203545/using-reflection-to-set-a-static

Why shouldn't I always use nullable types in C#

十年热恋 提交于 2019-11-29 01:32:11
问题 I've been searching for some good guidance on this since the concept was introduced in .net 2.0. Why would I ever want to use non-nullable data types in c#? (A better question is why wouldn't I choose nullable types by default, and only use non-nullable types when that explicitly makes sense.) Is there a 'significant' performance hit to choosing a nullable data type over its non-nullable peer? I much prefer to check my values against null instead of Guid.empty, string.empty, DateTime.MinValue