Sunday, August 23, 2009

inputbox in C#

I had to do a windows program, which is not that familier to me with .Net technology as I am ASP.Net guy. But it was really interesting than I thought before start the developments. In my early carrier I have done lot of developments using VB6.0 and in present too.
I wanted to add a input box in my new windows program which is in C#.Net, suddenly I realised that there is no input dialog box with C#.Net but I found a link to do a input dialog box, which was really helpfull. I think it would be really helpfull you guys as well.

http://www.knowdotnet.com/articles/inputbox.html
Wednesday, August 12, 2009

SQL LIKE THAN YOU LIKE



Do not confuse with the title, let me explain what it is. Consider about the following table, (Persons)







One might need to get only the FirstName that starts with letter ‘W’
in this case we can write our query as follows

SELECT * FROM Persons WHERE FirstName LIKE 'W%'

Result would be





OK cool , what if you need to get all the records that FirstName starts
with letter ‘W’ and letter ‘R’, in this case I am sure most of developes
query would be as follows.

SELECT * FROM Persons WHERE FirstName LIKE 'W%' OR FirstName LIKE 'R%'







Fine, you got the result what you expected but there is another way to do this
without using OR FirstName LIKE ‘R%’

SELECT * FROM Persons WHERE FirstName LIKE '[WR]%'

Don’t you think this is better? Keep in mind you can use this not only for the first letter
you can use this for last letter even you can use with negation ( LIKE ‘[^WR]%’)

Tuesday, August 11, 2009

Get percentage of records from a table

Have you ever come across a situation where you need to get top  25%
records from a table. Here is the SQL that returns first 25% of records from a table.

SELECT TOP 25 PERCENT * 
FROM dbo.yourtable

This will return what you want, the 25% of records from the table. What if you want the second 25% of records.

SELECT TOP 50 PERCENT *
FROM ( SELECT TOP 50 PERCENT * 
FROM dbo.yourtable) results ORDER BY 1 DESC


On the above query by descending the records by first column, assuming that it is the primerykey or the records we need to filter by, you will get the top  25% which is the second 25% of the records.

Think how to get the third and the last 25% records leisurely.

Thursday, August 06, 2009

ConnectionStringBuilders


Connection string is a string variable which include many properties and values (InitialCatelog, uid, pwd , Data Source) needs to connect to a database.

SqlConnection cn = new SqlConnection();
cn.ConnectionString ="uid=sa;pwd=Initial Catalog=ADO; Data Source=(local); ConnectTimeout = 30";
cn.Open();

Creating connection string is bit tedious as it’s a string and could be error prone.

ADO.Net providers support connection string builder objects which allow you to create connection string , and establish the name/value pairs using strongly typed properties.

SqlConnectionStringBuilder cnStrBuilder = new SqlConnectionStringBuilder();
cnStrBuilder.UserID = "sa";
cnStrBuilder.Password = "";
cnStrBuilder.InitialCatalog = "ADO";
cnStrBuilder.DataSource = "(local)";
cnStrBuilder.ConnectTimeout = 30;
SqlConnection cn = new SqlConnection();
cn.ConnectionString = cnStrBuilder.ConnectionString;

Here on above example , creates a SqlConnectionStringBuilder variable and passing values for
ConnectionStringBuilder properties and then the ConnectionStringBuilder’s ConnectionString value been assigned to cn.ConnectionString instead directly passing ConnectionString string
as follows.

cn.ConnectionString ="uid=sa;pwd=Initial Catalog=ADO; Data Source=(local); ";
Tuesday, August 04, 2009

C# File.WriteAllText, File.ReadAllText & File.WriteAllLines


Most of the times as developers we have to work with text files, as it’s a very simple way to store some data. There are very valuable methods available in File class that we are not using most often.

Look at the following example which writes a formatted string to a text file with new lines.
This is more readable than WriteLine() method. If you use WriteLine() Method then you will have to write line by line most probably you may have to use a loop.

protected void btnTextManipulation_Click(objectsender, EventArgs e)
{
//gets the file path
stringfilePath = Server.MapPath("Files/test.txt");
StringBuilder content = new StringBuilder();

//note the end of this line i.e \r\n this says enter a new line
content.Append("This is the first line. \r\n");
content.Append("This is the secont line. \r\n");

//Environment.NewLine this says enter a new line too
content.Append("This is the last line."+ Environment.NewLine);

//Writes to test.txt
WriteText(filePath, content.ToString());

//reads from test.txt and asssing to a variable
stringreadTxt = ReadText(filePath);

filePath = Server.MapPath("Files/test2.txt");
//Writes to test2.txt
WriteText(filePath, readTxt);
}

public void WriteText(stringpath, stringcontent)
{
//this file method writes all test you pass
File.WriteAllText(path, content);
}

public string ReadText(stringpath)
{
//this file method reads whole content of the text file.
returnFile.ReadAllText(path);
}


I will show another example which writes array items in new lines.In the following example WriteAllines() method writesall the items in an array as a new line. With using WriteAllLines() method you can avoid a loop, hich needs to loop through the array and WriteLine to the text file.



protected void btnTextManipulation_Click1(object sender, EventArgs e)
{
//get the file path
string filePath = Server.MapPath("Files/test.txt");
//declare and assign a string array
string[] content = { "This is the first line.", "his is the secont line.", "This is the last line." };
WriteArrayText(filePath, content);
}

public void WriteArrayText(string path, string[] content)
{
//this file method writes all test you pass
File.WriteAllLines(path, content);
}

My Achievements

Member of

Blog Archive

Followers

free counters