Tuesday, May 15, 2012

How to convert string to string[] in C#?

It might happen that you receive from POST a variable name that is in fact an array. So in this case you should convert your object into string[]. How do you do that?
Well, as usually, it is simple:
string[] RoomOptions = new string[] { Request["room_options"] };
So there you are.
This was for solving asp error message saying:
Error message: Cannot convert type 'string' to 'string[]'

Friday, May 11, 2012

How to make an update query using join in SQL

Today it helped me a lot this page which instructed me how to make an update query using join. So you can go and check it out on http://www.bennadel.com/blog/938-Using-A-SQL-JOIN-In-A-SQL-UPDATE-Statement-Thanks-John-Eric-.htm

Here you can see how I wrote my update command:


                        UPDATE  O
set O.Payed = Payed - 1, O.PaymentDate = null , refPaymentMethodId = null
from [Orders] O
join PaymentMethod pm on pm.PaymentMethodID = O.refPaymentMethodID
where OrderID = @OrderID and pm.GenerateReceipt = 1;
Cheers.

Thursday, April 12, 2012

How to use conditional operator(?:) in c# in a string?

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression.

The condition must evaluate to true or false. If condition is truefirst_expression is evaluated and becomes the result. If condition is falsesecond_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.
Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
This is what MSDN says. :)
Now: it might help you to know how to use conditional operator when generate a string. I will give you a simple method which receives a input parameter and according to that will generate a link.


public string GenerateLink(string Option)
    {
        string Response = "";
       Response = "<a href=\"YourPage.aspx?option=" + ((Option == "holiday") ? "goHoliday" : "goAndWork").ToString() + "\">Click</a>";
        return Response;
    }

So, don't hesitate to use conditional operator whenever you are in need of it.
Have a nice day.

Friday, April 6, 2012

How to verify if two strings are equal in Sql?

This question might appear to you when you want to verify credentials for a user at login.
I found out that for SQL these two conditions will give the same result.
1. if(@Password = 'test')
or
2. if(@Password = 'test ')
(note the space)
So there is a problem.
I found out a simple solution for comparing these two strings. When you compare strings you have to hash them. So we are going to use Hashbytes function. Here you can find more details.
For our example we can verify like this:
if ( HASHBYTES('MD5', convert(nvarchar(50), @Password )) = HASHBYTES('MD5', 'test ' ))
will return expected result.
One think to note: it is important to keep in mind datatype. Datatypes must be the same otherwise you won't get the same hash. Md5 has in mind even data types when converting ;)

Hope will help you.

Friday, March 9, 2012

How To Allow Blocked Content on Internet Explorer

I thought that will be great if I will post here on this blog for anytime when I found an article on the internet that helped me. So based on this idea I am going to give you a short description of problem(found in title, maybe) and the link to the solution.
So one of my question of today was: How To Allow Blocked Content on Internet Explorer?
Instructions found you can get at  http://www.genopro.com/help/report-generator/allow-blocked-content/
Regards

Friday, March 2, 2012

How to validate if an email address is syntax valid in C#?

As a simple question: how do you validate if an email address is correct? (by syntax point of view)
If you need this function, to validate an email address you should use this method which is using regular experssions:
public bool IsEmailSyntaxValid(string emailToValidate)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(emailToValidate,
                @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

Thursday, March 1, 2012

How to disable textarea resizing with Css?

Today I had to make `something` so my textarea field could not be resized. There might be different reasons why a user should not resize a textarea(which is a default setting in most browsers).
So if you want your textarea to be fixed, no rezise just use this in your css style:

    
.your_div textarea {
    resize: none;
}

There you go :)
Regards