Archive for the ‘.NET’ Category

How to enable VSS over the Internet

Tuesday, August 19th, 2008

I found this great tutotial that I recommend: http://alinconstantin.homeip.net/webdocs/scc/VSS_Internet.htm

Using parameters for Ms Access query in VS query builder

Thursday, July 31st, 2008

It is difficult to use a parameterized query in VS to get data from a mdb database. You usually have to use ? instead of the parameters, but if you want to use the parameter more than once (like in an iif function or something like that), then you should probably need to use the name of the parameter.

To make a long story short, just use [@name]. Notice both [] and @ signs.

Reading Application Settings

Friday, May 23rd, 2008

I wanted to use the connection string that was automatically generated, so I googled for a solution and I found this: System.Configuration.ConfigurationSettings.AppSettings[name of item].

Unfortunately, when building, you get “System.Configuration.ConfigurationSettings.AppSettings’ is obsolete”. This is because Microsoft has removed the AppSettings class from the System dll. The best solution I have found is here:

System.Configuration.ConfigurationManager. AppSettings[name of item]

And remember to add a reference to System.Configuration.dll.

Display decimals for a number

Friday, May 9th, 2008

The .ToString method has some wonderful features. If you want to display only 2 decimals for a number, you just have to display the format, like this: yourNumber.ToString(”N2″).

I found this solution on http://www.geekpedia.com/Question10_How-can-I-format-a-number-to-x-decimal-places.html. You can find there more formatting methods, in case you have some strict preferences.

Setting the Culture for a Windows Application

Wednesday, April 16th, 2008

Some of the operating systems that we install our applications on, have the Regional Settings set to Romanian. So there can be problems with the numbers and date formatting. The best sollution is to set your application culture, no matter what the OS culture is:

using System.Threading;
using System.Globalization;
// Put the following code before InitializeComponent()
// Sets the culture
Thread.CurrentThread.CurrentCulture = new CultureInfo("ro-RO");
// Sets the UI culture
Thread.CurrentThread.CurrentUICulture = new CultureInfo("ro-RO");

The code is taken from http://msdn2.microsoft.com/en-us/library/b28bx3bh(VS.71).aspx.

Conversion failed when converting the varchar value ‘@parameter’ to data type int or something like this

Friday, April 11th, 2008

This is one of the most annoying errors that you now is easy to handle, but always have to google the sollution, because you don’t remember what you did last time to get rid of it.

Stop checking the C# code, the problem is not there. Just remove the quotes from the query string in the stored procedure and it will work just fine:).

DataGridView - setting error message for wrong data in ValueType columns

Tuesday, April 8th, 2008

I had to write an application where I used a DataGridView with various DataTypes columns. For the dateTime column I used a Calendar Column, but for the Int32 columns, I wanted to display a message that would say to the user that they inserted some wrong data. I have found an example on http://www.codeguru.com/forum/showthread.php?t=418306 , but the example is in VB.NET. I wrote something in C#:

private void callOnEdit(object sender, DataGridViewDataErrorEventArgs e){
try
{
switch (e.ColumnIndex) {
case 1: MessageBox.Show(“Message.”);break;
//and so on
}
}
catch
{
MessageBox.Show(
“Message!”);
}
}

To use this, you only have to go in the Events panel for the DataGridView and set the DataError event to be handled by this function.

Please note that in VB.NET you can use “Handles”, but in C# you can’t. This is why:

http://www.velocityreviews.com/forums/t367616-using-quothandlesquot-vb-vs-c.html

Adding a Calendar column in a DataGridView (C#)

Tuesday, April 8th, 2008

If you have a DataGridView and want to be fancy and add a calendar column, here is the code (I found it on http://msdn2.microsoft.com/en-us/library/7tas5c80.aspx):

using System;
using System.Windows.Forms;

public class CalendarColumn : DataGridViewColumn
{
public CalendarColumn() : base(new CalendarCell())
{
}

public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CalendarCell)))
{
throw new InvalidCastException(“Must be a CalendarCell”);
}
base.CellTemplate = value;
}
}
}

public class CalendarCell : DataGridViewTextBoxCell
{

public CalendarCell()
: base()
{
// Use the short date format.
this.Style.Format = “d”;
}

public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CalendarEditingControl ctl =
DataGridView.EditingControl as CalendarEditingControl;
ctl.Value = (DateTime)this.Value;
}

public override Type EditType
{
get
{
// Return the type of the editing contol that CalendarCell uses.
return typeof(CalendarEditingControl);
}
}

public override Type ValueType
{
get
{
// Return the type of the value that CalendarCell contains.
return typeof(DateTime);
}
}

public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return DateTime.Now;
}
}
}

class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public CalendarEditingControl()
{
this.Format = DateTimePickerFormat.Short;
}

// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
return this.Value.ToShortDateString();
}
set
{
if (value is String)
{
this.Value = DateTime.Parse((String)value);
}
}
}

// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}

// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}

// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}

// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return !dataGridViewWantsInputKey;
}
}

// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}

// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}

// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}

// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}

// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}

protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}

Ok, now to add it to your DataGridView, you can do like this:

CalendarColumn col = new CalendarColumn();
col.Name = “data”;
col.ValueType = typeof(System.DateTime);
col.HeaderText= “Date”

yourDataGrid.Columns.Add(col);

Just a quick word

Tuesday, April 8th, 2008

Hi and welcome to our blog!

We are a team of PHP and C# (sometimes VB.NET) programmers. You will see here some code parts that helped us in our work. Some of the code we will post here is not our work, we found it on the internet, but we will add the links to the original locations.

We hope this will help you!

Best regards,

The Soft Build Team