HTML/JavaScript: Dynamically populating combobox/select options

I got a problem that how to dynamically populate values in html combo box using javascript. Or rather say, how to dynamically add options in select element of html.
here is our html code:

<select id="id_s">
 <option value="a">first</option>
 <option value="b">second</option>
</select>

Now suppose i want to add one option ‘third’, here is the code for it:

var combo = document.getElementById('id_s');
combo.options[2] = new Option ("third", "c");

Courtesy : This forum.

Advertisements

C# Static class and constructor: constructor in static classes in c#

Static classes in C# doesn’t have instance constructor(constructor used to initialized instance of a class) since it can’t be initialized. Static class is initialized only once when they are used first time in application.

So i was wondering how we can initialize a member if really we need. The answer is using static constructors.

Here is the example of how you can use static constructors in your static class:

static class StaticClass
{
static StaticClass()
{
System.Windows.Forms.MessageBox.Show(“I m crated”);
}
public static String Hi()
{
return “hi”;
}
}

Here, messagebox will be shown when first time StaticClass.Hi() method will be called.

Note: static constructors could be used in normal classes also.

C# tips & tricks: Do you want to rethrow exception ?

Many times during development we really don’t want to handle exception every where, rather we like to throw it again. But what is the best way to do it:

catch(Exception ex)
{
throw;
}

OR

catch(Exception ex)
{
throw ex;
}

This article I found in net gives really good insight of it. It suggests to use first method, as it would not truncate previous stack trace information.

C# tips & tricks: String Concatenation, Best practices

When we are struggling with String concatenation issues, specially which is the best way to concatenate the strings. One of my colleague suggested following articles to me.

though this may not be giving every thing you are looking for, but really very nice and worth readable for the .NET developers.

ASP .NET: Dynamically creating/generating excel file at server and making it available to download

If you want that when user click on a particular link, based on his preferences an excel file should be generated dynamically and should be downloaded automatically at client. Here is small example how you can do it.

Trick is create a html file and present it as excel file ;).

for this follow the steps:

1.  Add a web form say xlImage.aspx, in your project.

2. Delete every thing from xlImage.aspx page except first line.

3. Now in code behind page, i.e., in xlImage.aspx.cs add following line on,

Page_Load function

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();

Response.Buffer = true;
Response.AddHeader(“content-disposition”, “attachment;filename=file.xls”);

Response.ContentType = “application/vnd.ms-excel”;

String imagepath = “<img src=’http://localhost:2290/Excel%20Export/pic/1.jpeg&#8217; width=’70’ height=’80’ style:’position: absolute;left:200px;’/>”;

Response.Output.Write(“\n<body>\n<html>”);

Response.Output.Write(“<center><table width=’800′ align=’center’ style=’text-align:center’ border=’1px’>”);
Response.Output.Write(“<tr><td></td><td></td><td></td><td></td><td colspan=’2′ align=’center’><div>” + imagepath + “</div></td></tr>”);
Response.Output.Write(“</table></center>”);
Response.Output.Write(“\n</body>\n</html>”);

Response.Flush();
Response.End();

same thing could be done for word file also.

Win32 API listings

I was wondering any one has put win32 api listing online. I googled and found some really useful resources:

Published in: on August 7, 2009 at 11:15 am  Leave a Comment  
Tags: , ,

Hiding grid lines in Excel

This was the thing i want to do in excel. Hiding all the grid lines and showing only tables which i have created.

Hiding grid lines:

1.  Go to tools-> Options… Menu

2. It will show Options dialog box.

3. Go to View tab.

4. In view tab, at down Window Options part, uncheck Gridlines checkbox.

Published in: on August 6, 2009 at 4:49 pm  Leave a Comment  
Tags: , , ,

C#:Handling Enter key press event for textbox.

If you want to do something when focus is on textbox and and user press ‘Enter’ key, do the following way:

private void txt1_KeyPress(object sender, KeyPressEventArgs e)</code>
{
 if (e.KeyChar == (char)Keys.Enter)
 {
    //do something
 }
}

where txt1 is textbox

Published in: on August 5, 2009 at 3:31 pm  Leave a Comment  
Tags: , , ,

MySQL:Can’t connect to MySQL server (10060) from remote client, on windows xp.

I tried connecting to MySQL Server using MySQL query browser but i was getting above error. I found following solution in MySQL forum.

http://forums.mysql.com/read.php?34,49742,239961#msg-239961

I found the problem: i had not added the MySQL Server port in firewall’s exception list.

//<———–part 2

Actually there is no MySQL error with no. 10060. The actual error no. is at the begninning of the error message.(may be ERROR 2003 etc.)

Typically this kind of error message you see due to authentication issues. There could be many reason:
1. The MySQL server isn’t running.
2. Port at which MySQL server is listening is blocked for you.
3. Port at which MySQL server is listening cann’t accept any connection from outside world due to Firewall.

Actually if it is a problem due to Firewall, no need to turn off firewall, you just have to include MySQL listeining port in to unblock/exception list of firewall.

If your MySQL server is running on windows xp, following is the way to deal with firewall problem:
1. Go to Control Panel -> Windows Firewall (Windows Firewall dialog gets open.)
2. Go to Exceptions Tab and click on ‘Add Port…'(Add a Port dialog gets open.)
3. Give the appropriate name.(e.g., MySQL)
4. Give the listening port of MySQL.(Most commonly 3306)
5. Also make sure TCP radio button is selected.

Here is the complete tutorial on how to add a port to Firewall exception list on windows xp.

If sill problem persist refer MySQL documentation, on mysql website for problem regarding MySQL server connection problem.
//———->
modification history:

1. Nov 18, 2009: Due to lot of visit to this post, i added more detail as part 2.

please let me know if this post is useful.

MySql: Give Root User Logon Permission From Any Host

Following article describes you how to do this. I was not able to connect with MySQL server using MySQL Query Browser from remote machine then i got this article.

http://www.howtogeek.com/howto/programming/mysql-give-root-user-logon-permission-from-any-host/

You can do the same from query browser. Just edit the MySQL.user tables host column. Here for any user you can give the client name which you are expecting or just give ‘%’ if you are want to connect from any host.