Friday, February 29, 2008

Get Logged in Windows User Name in ASP.NET Page

In order to get username of windows user who is logged into ASP.NET website, I used

string winUserName = System.Web.HttpContext.Current.User.Identity.Name;

winUserName contains the name of the user along with domain name.

Wednesday, February 27, 2008

Access the Items of the DataList ASP.NET

Today I learned how to access the selected row items of the datalist. In ItemTemplate of datalist I have added the button and on button click event I can access the items (e.g. MyLabel item of datalist named MyDataList). The steps are as follows

1. Add the button to the Itemtemplate of Datalist
2. enter 'select' in the CommandName property of the button
3. In Datalist SelectedIndexChanged add the following code. The value contained in item MyLabel is stored in the Mystring.

string MyString;
MyString = ((Label)MyDataList.Items[MyDataList.SelectedIndex].FindControl("MyLabel")).Text;

Tuesday, February 26, 2008

ADO.NET Hello World !!!

Today I learned how to connect to Microsoft SQL database from ASP.NET page using ADO.NET.
Here is the Page Load method which connects to the database and assigns the values taken from the database to the dropdownlist control.

protected void Page_Load(object sender, EventArgs e)

// SQL Connection
SqlConnection MyConnection = new SqlConnection();
MyConnection.ConnectionString = "Data Source=COMPUTERV22;Initial Catalog=CIMNET2;User ID=sa;Password=manager;";

//SQL Command
SqlCommand MyCommand = new SqlCommand();
MyCommand.Connection = MyConnection;
MyCommand.CommandText = "Select * from Employees";

//Open a connection to the database

// SQL Reader, place where rows from database is stored.
SqlDataReader MyReader;
MyReader = MyCommand.ExecuteReader();

// Read each record from the database
while (MyReader.Read())

//Close the connection to the database