iAlpine.net
A journey of exploration and discovery

C# example of a wroking datetime format in MS-SQL.

February 8, 2010 13:06 by mihko


Formatting of a DateTime type can sometimes be a tricky thing when working with C# and MS-SQL data. This is due to many different SQL collation settings and also datetime standards.

Since SQL has a DateTime standard in the format: 2010-01-20T00:00:00, everytime I deal with it in C# I use a String.Format method for conversion:

Example:

String.Format("{0:s}", Datetime.NOW);

With this method I am sure that MS-SQL will deal correctly with my DateTime.


Visual Studio 2008 Workaround - Microsoft Jet OLEDB Provider for Excel is not supported under 64-bit versions of Windows.

October 29, 2009 00:58 by mihko


I have run across this problem now for the second time. Now in application that I have built in Visual Studio. When I tried running my already working application that imports data from Excel into SQL Server database on a 64-bit machine the functionality of importing didn't go through anymore.

A workaround here that I found and works again as in SSIS is that you set it to run under 32-bit. So if you have this in Visual Studio 2008 in properties of project, build menu set Platform target to 32-bit (x86). Application will now be run under 32-bit and OLEDB provider will work fine.  I found no other solution yet of how to build it and run it in 64-bit environment.


C# - Unable to read the stream from files using Resources in Windows Forms project?

October 16, 2009 15:49 by mihko


A few days ago I needed to access and read the stream from files that I included in my Resources of Windows Forms project. On Microsoft support website http://support.microsoft.com/kb/319292 is a nice article about "How to embed and access resources by using Visual C#". Make sure you read all of it.

A process is straight forward using Assembly namespace, the trick that I encountered in my project was that in my debugger I kept on receiving a null value when reading any type of files to stream using:

// Reading stream from Aero Silver-Busy.ani Cursor
Stream my = this.GetType().Assembly.GetManifestResourceStream("demoProject.Resources.Aero Silver-Busy.ani");

Later after rereading the article I discovered that in Resources you need to click on a file and in Properties of a file set Build Action property to Embedded Resource. That is it and make sure you are refering to the right namespace and filename!


C# - Check if your string reference variable is null

August 19, 2009 22:32 by mihko

A new handy method that I just discovered in C# and I wish I knew it before is: String.IsNullOrEmpty(string value).

Whenever you are checking your string variables for null reference value you can just use this method instead of directly comparing your variable to null.  I use this quite often when I needed to pass to my parameters either a string value or a null.

Example: 

A method below will check if your string variable is null and therefore return null or a string value.


// Method that checks if string reference value is null
private string IsStringNull(string ValidString)
{  
   
if (String.IsNullOrEmpty(ValidString) == true)
   { ValidString =
null; }
  
else 
  
{ ValidString = ValidString; } 
   return ValidString;
}


XtraReports - Preview Report during design time while passing parameters from SQL Stored Procedure.

July 9, 2009 17:15 by mihko


When building reports using Developer Express - XtraReportsSuite and passing the paramerers from stored procedures to the report, it seems that you need to use the sqlDataAdapter instead of TableAdapters to have the functionality of previewing reports working during designing time in Visual Studio.

By default the sqlDataAdapter is not included in VS Toolbox so you need to add it from the items in .NET Framework Components. After setting the properties of Select Command, don't forget to define also the parameters in Parameters collection properties and add it's testing values.


Windows Forms Microsoft ToolTip Binding to SQL to show data & memory performance

April 25, 2008 12:27 by mihko


I found out on my laptop where I have installed locally SQL 2005 and VS 2008 that when implementing ToolTip in Windows Forms Application that the ToolTip component can be quite memory demanding. I have encountered this problem in an application where I have a datagridview bound to data in SQL. When adding a ToolTip feature and databound some information from SQL to ToolTip component the application started consuming a lot more CPU power and it was performing visibly slower.

The only solution I know until know is to try to avoid information bound to ToolTip. In my application I displayed the same information on click event in MessageBox.  


Saving Null Values to DateTime field in SQL with Windows Forms TextBox Field

October 3, 2007 17:19 by mihko

Saving Null values into DateTime Fileds in SQL Database is not as straight forward as it seems in C# and Windows Forms.  If you want to save a Null value into Database you have to write a custom code otherwise you encounter various interesting errors.

To solve a problem in one of my projects I used the following code:

Step 1:
Because I wanted that Data is saved when DataGridView Row Validating Event fires I wrote the folowing code in DataGridView_RowValidating event.


// Save Data to Tables on RowChange
private void vozilaDataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{

// If field TxtDatumNakupa is blank
if (TxtDatumNakupa.Text == "")
{
try
{
// Define Nullable DateTime myDate
DateTime? myDate = new DateTime();

// Set it to Null
myDate = null;

// Update the filed in DataSet with null value and toolStripStatusLblVoziloID.Text is the ID where Update occurs in DB
vrednost_VozilaTableAdapter.UpdateQueryDatumNakupaUpdate(null, Convert.ToInt32(toolStripStatusLblVoziloID.Text));

}

catch (FormatException)
{
MessageBox.Show("Error writing to DB!");
}

}
}

Step 2:
Since the TextBox that is bound to DateTime also needs to be validated for correct input I used a TextBox Validating Event to check for correct DateTime format

private void TxtDatumNakupa_Validating(object sender, CancelEventArgs e)
{


// Define date
DateTime date;

// If its not same format as date and txtDatumNakupa.text is also not empty
if (!DateTime.TryParse(TxtDatumNakupa.Text, out date) && TxtDatumNakupa.Text != "")
{
MessageBox.Show("Date in such format does not exist!", "Row Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Cancel =
true;
}
 
}

Step 3:
Because TextBox is DataBound and it does not let click out of a TextBox if it is empty, I found that in Design View where DataBinding is done to TextBox you have to add DataSourceUpdateMode.OnPropertyChanged, "" so it lets you pass by with an empty field.

this.TxtDatumPogodbe.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.vrednost_VozilaBindingSource, "Datum_Pogodbe", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged, ""));

That is it! When I tested the example I was able to normaly save Null value into SQL.
* Note that Nullable DateTime myDate does not need to be declared here I left it inside in case you want to save also a DateTime below and modify example a bit.






  

Sponsors


www.knetit.net

Kiva - loans that change lives


  

Job Board for Web Designers and Web Developers

The On Demand Global Workforce - oDesk

affiliate_link