Thursday, September 30, 2010

SQL "not" for a variable

in c# i have a:

a = !a
(if false makes it true, if true makes it false)

in sql i want to do the same with a BIT variable, something like:


declare @a bit
set @a = 1
select @a
set @a = not (@a)
select @a


can i?

i could always do an IF, but this would "look better" :)

with the help of stackoverflow.com:
you can do either:
1. @a = @a ^ 1
or
2. @a = ~@a

i personally prefer #2

Monday, September 20, 2010

Friday, September 3, 2010

Tuesday, August 31, 2010

Textbox validate characters digits only - Desktop App

I wanted to let the user enter onlydigits in a Textbox and validate the characters when the types.
I came up with this:





private void numericUpDown_Count_KeyPress(object sender, KeyPressEventArgs e)
{
if ("1234567890".IndexOf(e.KeyChar.ToString()) > 0)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}





tell me if there's an easier way :)

Wednesday, June 16, 2010

Citrix: how do you ctrl + alt + delete ?

in Citrix, the ctrl + alt + delete can be done like this:
ctrl + f1

Tuesday, May 11, 2010

The ConnectionString property has not been initialized

I had this error:

The ConnectionString property has not been initialized.

I have a MSI installer, that is installed in our QA environment.

It was working before, and suddenly, I only had this error.
Googled up a bit, all this info on how to get the connection string and all that.

I finally realized that I rebuilt the whole solution, but I didn't rebuild the Deployment project. After a rebuild ... everything back to normal.

#:-S

Hope this helps someone.

Thursday, April 1, 2010

Gridview nowrap

What do you do when you have
AllowPaging="False" AllowSorting="False" AutoGenerateColumns="True" Width="800px" ShowHeader="true"

And it wraps all your data?

you add:

OnRowDataBound="GridView_RowDataBound"

and then:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
}
}

Tuesday, March 2, 2010

Enum to DataTable

I had a lot of ENUM-s and needed to make DataTable - s with the same structure for pretty much all of them.
I came up with this:




private static DataTable CreateDataTable < TheSQLtable > ()
{
Type t = typeof(TheSQLtable);
if (!t.IsEnum)
{
throw new InvalidOperationException("Type is not Enum");
}

DataTable dt = new DataTable();
dt.TableName = t.Name;
DataColumn dc;

string[] names = Enum.GetNames(t);
foreach (string name in names)
{
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = name;
dc.Unique = false;
dt.Columns.Add(dc);
}

return dt;
}





(with a little help from stackoverflow)

Wednesday, February 24, 2010

How to check in C# if a DataRow is Empty. (nothing in any column)

I was hoping i find something like dr.isEmpty(), but there's nothing "this nice". So you do something plain like:





bool AreAllColumnsEmpty(DataRow dr)
{
if (dr == null)
{
return true;
}
else
{
foreach(var value in dr.ItemArray)
{
if (value != null)
{
if (value.ToString() != "")
{
return false;
}
}
}
return true;
}
}