Wednesday, August 06, 2008

Locate Link using XPATH and Link Text

If you want to click on link on HTML with the text displayed as 'Sign out' use following for Selenium RC using C#

selenium.Click("//a[contains(text(),'Sign out')]");

Friday, August 01, 2008

Selenium Image Attributes

Get alternate text from image from HTML document. I use here Xpath query to locate image in the HTML Document

String deviceStatus = selenium.GetAttribute("//img/@alt");

Thursday, July 10, 2008

Delete Directory using C#

In order to Delete Directory using C#, use following syntax

System.IO.Directory.Delete(directoryPath, true);


The true varibale delete all contents of the directory (recursively)

Tuesday, June 24, 2008

Required Field Validator for DropDownList

You can use RequiredFieldValidator for DropDownList.

Just select InitialValue of the RequiredFieldValidator to -1 or 0 whatever that you select for the first element (often with text "Please Select") of DropDownList. Now validator will work and you need to select any other value than the InitialValue in order to pass validation.

Tuesday, June 17, 2008

Default Item for the DropDownList

You can bind C# dropdownlist to datasource and then assign the default value to it using following syntax. By default the 0th item is selected but you can specify it explicitly if you want to select item at different location.

dropDownList.Items.Insert(0,"Please Select"); // Inserts at the first location
dropDownList.Items[0].Selected = true; // First location is selected

Tuesday, May 27, 2008

File Upload Limit in ASP.NET

Using ASP.NET fileupload control one can upload by default size upto size of 4 MB which is mentioned in machine.config. I overrided it in web.config and I was able to upload file with size as mentioned by maxrequestlength property. (20480 ≈ 20 MB)

<system.web>
<httpruntime maxrequestlength="20480" executiontimeout="240">
</system.web>

Friday, May 16, 2008

Enable SQL Authentication for MS SQL 2005 Server

In Order to enable SQL authentication for MS SQL 2005 server, I did the following steps

1. Open SQL Server 2005 Surface area configurator and opened Surface area configuration for services and connection. Under database engine, remote connections I enabled Local and Remote connections.

2. In SQl management studio I logged in to SQL server on localhost and right clicked on properties and went into security tab and enabled both Windows and SQL Authentication.

3. Later under the localhost node in management studio, clicked on security\logins folder and created new login using right click.

4. In case the account you created is disabled, make new query as follows (where login name is 'sa')

ALTER LOGIN sa ENABLE
GO

Friday, May 09, 2008

Load ASP.NET UserControl programatically

In order to load UserControl programatically into view control of Multiview control of ASP.NET, I wrote the following piece of code

ViewName.Controls.Add(LoadControl("~/UserControlName.ascx"));

Thursday, April 03, 2008

Display Multiline Text from the Database on ASP.NET webpage

In order to display multiline text from the database, I used the following code. I used textbox and removed its borders (set BorderStyle= None and BorderWidth= 0px) and scrollbar (To hide scrollbar, added style="overflow:hidden in ASP textbox tag).


string newsBody = GetText(); // From the database

NDNewsBody_TextBox.Rows = newsBody.Length;

NDNewsBody_TextBox.Text = newsBody;

Monday, March 31, 2008

Bound custom data to the Column of the GridView

Today I learned how to attach custom value to the column of the GridView control in ASP.NET page. What I did is as follows

First I click on Edit Templates of the GridView and inside ItemTemplate I dragged one Hyperlink control. For that Hyperlink's NavigateUrl and Text I used databindings.

Later I added new column to the GridView and selected TemplateField. Now the template field column in the GridView shows the Hyperlink whose URL and Text are bound to the custom date.

Unique Identifier Generator in .NET

The following code generates the Unique Identifier in string format..
Guid.NewGuid().ToString();

Wednesday, March 26, 2008

Get Columns Names of the MS SQL Table

Following MS SQL query gives the list of column names of the table_Name

select col.name from sysobjects obj inner join syscolumns col on obj.id = col.id where obj.name = 'table_Name'

Wednesday, March 19, 2008

RequiredFieldValidator and Button Click Event

If you use RequiredFieldValidator control on your ASP.NET page, every button click will perform validation. If you have rest buttons on your page on whose click you dont want validation, then set CausesValidation property of button to false

Thursday, March 13, 2008

Passing values between ASP.NET pages

Today I learned how to pass values between two pages in ASP.NET. Lets say you have Page1.aspx and Page2.aspx and want to pass values from Page1.aspx to Page2.aspx. You can put link button control on Page1.aspx and on its click event you can write this code

string mydata = "88";
Response.Redirect("Page2.aspx?value=" + mydata);

The above code will open Page2.aspx and now in order to get the value, you can write code on PageLoad event or any other method in code-behind file of Page2.aspx as follows

string mydata = Request.QueryString["value"];

Tuesday, March 11, 2008

Substring of DataList field

The label control which is bind to the field for DataList control of ASP.NET page can be truncated using following syntax

Eval("FieldName").ToString().Substring(1,20)

Monday, March 10, 2008

Traverse through SQL Table rows

Here is the code to traverse (iterate) through each row in SQL table...

DECLARE @i INT
SET @i = 1

WHILE (@i<= (SELECT count(*) FROM table_name) )
BEGIN
/* Your Code here...*/
SET @i=@i+1
END

Monday, March 03, 2008

Event Trigger on selection changed in the DropDownList

On ASP.NET page....In order to tigger the event when selection of the DropDownList changes...write code in SelectionIndexChanged event of the DropDownList and dont forget to enable AutoPostBack to True.

Empty TextBox Check using RequiredFieldValidator

In order to check if something is entered into textbox...just drag and drop RequiredFieldValidator user control and set its ControlToValidate property to the desired textbox. And enter the custom error message you like to display...

Now when text is not entered into the desired textbox and page is refreshed using button click event the custom message appears......

Absolute Positioning of User Controls on ASP.NET Page

In Order to position ASP.NET usercontrols in visual designer...select control and then Click ( in Visual Studio ) on Layout->Position->Absolute. And then select and move the control on ASP.NET page as you like..

RadioButton GroupName

Simple and Silly but still important...Today I learned how to choose only one radio button among the radio buttons that are placed on the ASP.NET page. Just give the same GroupName (just any text) property of all radio buttons which you either want to select.