Posts

Donate

Unknown database 'your_database' error In MySQL In app.config

There's a weird scenario when retrieving table names from a specific database, upon querying, using connection.Open() in C#, an error such as "Unknown database 'database_name'" shows in the exception object. I was simply checking the database name using MySQL Browser and found out that the database name starts with a space. So, after thinking about it, I decided to modify the app.config and changed the value of the connection string by enclosing the name with single quotes to include the space character of the database name. Not working app.config setting: <add key= "try" value= "server=127.0.0.1;database=database_v1;Uid=james;pwd=pass123;" /> Working app.config setting with database name enclosed in single quotes: <add key= "try" value= "server=127.0.0.1;database=' database_v1';Uid=james;pwd=pass123;" /> Note: This fix is applied towards database names that starts/ends with space. Cheers! Th

ASP.NET MVC AJAX Sys is undefined error (ASP.NET MVC 2.0 Remote Validation)

Referencing a remote validation can be a pain in the arse since several javascript errors will pop up such as stated in the title. So, in order to remove those unwanted script errors in ASP.NET MVC 2.0, use the solution below. <script src= "<%= Url.Content(" ~/Scripts/MicrosoftAjax.debug.js ") %>" type= "text/javascript" ></script> <script src= "<%= Url.Content(" ~/Scripts/MicrosoftMvcValidation.debug.js ") %>" type= "text/javascript" ></script> <script src= "../../Scripts/RemoteValidationScript.js" type= "text/javascript" ></script> Note: RemoteValidationScript.js is an external Validation javascript file. The referencing of scripts should be in order to avoid null or property not found javascript exceptions(RemoteValidationScript should be referenced last). You may also use (../../Scripts/MicrosoftAjax.debug.js) for long method. Reference: Sys

Pass GUID As Parameter To Action Using ASP.NET MVC ContribGrid

Hi, In a scenario where in you want to pass a GUID object to an Action Parameter, the parameter might contain a null value. Since Guid is not nullable, you can't simply just pass it to an action parameter because Action parameters require nullable and reference types. You might encounter an error like this: An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter Assuming you have an aspx view below: <div> <% Html.Grid(Model).Columns (c => { c.For(m => m.Price); c.For(m => m.ProductName); c.For(m => m.ProductID); c.For( "View Person" ).Named( "" ).Action(m => { %> <td> <%= Html.ActionLink( "View Person" , "ProductDetail" , "Home" , new { @id = m.ProductID.ToString() }) %> </td> <% }); %> <% }).Render(); %> </div> and an action

Embedding A DataGridview In Combobox Item In Windows Forms

Image
Hi, There's a post in codeproject that will host a datagridview in a combobox. It is in vb.net and I converted it to C#. I made some changes on the custom controls to retrieve the datagridview selected row. This was not provided in the author's post, so I made some changes myself. In total, the control was purely awesome. So, here's the C# equivalent. I won't be posting all the codes since the custom control is posted in codeproject. I'll be posting the main class instead. Credits: Niemand25 of Lithuania using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; namespace MyGridComboBoxCSharp { [ToolboxItem(true)] [ToolboxBitmap(typeof(ComboBox))] [DefaultBindingProperty("SelectedValue")] [LookupBindingProperties("DataSource", "

ASP.NET MVC String.Format() Not Showing Correct Format In Views Using jQuery Calendar

Image
Greetings, Formatting display output of dates in asp.net mvc should work using String.Format() with custom date formatting criteria. However, applying String.Format() in asp.net ascx view does not display the desired format that I want. Example markup: @Html.TextBoxFor(model => model.HireDate, String.Format( "{0:g}" , Model.HireDate)) Given Date Input: 2/6/2013 12:00:00 AM Desired Output should be with time portion removed: 2/6/2013 Solution: The trick to display the desired format was to decorate a display format attribute in my model class HireDate property: private DateTime hDay; [DisplayName("Hire Date")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public DateTime HireDate { get { if (hDay == DateTime.MinValue) { return DateTime.Today; } else return hDay; } set { if ( value == DateTime.MinValue) { hDay = DateTime.

Clean Invalid XML Characters In C#

Here's a cool way to clean Large XML files with invalid xml characters. Note: Stream from is the original xml file, while Stream to is the new xml file with invalid characters removed. private void Copy(Stream from , Stream to) { TextReader reader = new StreamReader( from ); TextWriter writer = new StreamWriter(to); writer.WriteLine(CleanInvalidXmlChars(reader.ReadToEnd())); writer.Flush(); } public static string CleanInvalidXmlChars( string text) { string re = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]" ; return Regex.Replace(text, re, "" ); } Source: http://social.msdn.microsoft.com/Forums/ Post: Invalid character returned from webservice

NBuilder Simple Example In C#

Nbuilder is a great tool in developing mock builders to generate data out of you application. Assuming, you don't use database for retrieving records, you can use NBuilder with LINQ capabilities to generate test data. Assuming if you have a class in your model like this: namespace MvcApplication1.Models { public class Product { public int Id { get ; set ; } public string Name { get ; set ; } public double Price { get ; set ; } public string Description { get ; set ; } } } To generate a product object using NBuilder, you may use something like this: Product product = Builder<Product> .CreateNew() .With(x => x.ProductId = productId) .And(x => x.Price = Convert.ToDouble(productId)) .And(x => x.Name = "Mushroom Can" ) .And(x => x.Description = @"Imported from New Zealand. Fresh and Juicy." ) .Build(); Cheers!

Debugging jQuery Or Javascript Code Not Working In ASP.NET MVC 2 (VS 2010)

In an application where there is internal jquery code in your MVC project, placing a breakpoint won't work when you want to enable debugging mode. After searching, a workaround has been provided by Microsoft Team. The solution is to transfer the javascript/jQuery script to an external .js file instead of embedding it in your .aspx script.Refer to the workaround. Solution: http://connect.microsoft.com/VisualStudio/feedback/details/652428/mvc-3-mvc-2-debug-ignores-javascript-breakpoints Cheers! Greg

How To Change Position A Windows Forms MessageBox in C#

Here's a tip from CODE PROJECT on positioning message box. This utilize c++ dlls. using System.Runtime.InteropServices; using System.Threading; [DllImport("user32.dll")] static extern IntPtr FindWindow(IntPtr classname, string title); // extern method: FindWindow [DllImport("user32.dll")] static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool rePaint); // extern method: MoveWindow [DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect); // extern method: GetWindowRect void FindAndMoveMsgBox( int x, int y, bool repaint, string title) { Thread thr = new Thread(() => // create a new thread { IntPtr msgBox = IntPtr.Zero; // while there's no MessageBox, FindWindow returns IntPtr.Zero while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ; // after the while loop, msgBox is

ASP.NET MVC 404 Content Page Showing Or Appearing Twice

Image
Good evening gents, In handling 404 pages (pages that don't exist in the website),I encountered a problem wherein the asp.content tag/control would show twice in the page. Here's the server content code: <asp:Content ID= "Content2" ContentPlaceHolderID= "MainContent" runat= "server" > <h2>Page Not Found!</h2> <br /> Error in URL Navigation. The page you were looking for was not found. </asp:Content> And here's the content being displayed twice. The code I'm using was from a documentation/series on how to handle 404 pages. The tutorial has a controller factory that checks invalid routes and calls the base controller if a route is invalid. Below is the code: //inherit Controller Class public class BaseController : Controller { private string context; public string MyContextUrl { get { return context; }

Donate