ASP.NET

Get root path, root url and any url of your website asp.net

This technique will help you to get url and path of server that you want.
Let’s see this result.

When I place this script on this path below :

http://www.aspgod.com/test/mappath/Default.aspx

And the script is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Request.Url.AbsoluteUri  //  http://www.aspgod.com/test/mappath/Default.aspx
Request.Url.AbsolutePath  //  /test/mappath/Default.aspx
Request.ApplicationPath  //  because I'm not set the virsual directory indeep of the server
 
Server.MapPath("") // d:\xxx\zzz\aspgod.com\httpdocs\test\mapath
Server.MapPath("Default.aspx") // d:\xxx\zzz\aspgod.com\httpdocs\test\mapath\Default.aspx
 
if you want to get the root path and root url.
You must enter this script below:
 
Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath,string.Empty) + Request.ApplicationPath
//  www.aspgod.com/
Server.MapPath(this.Request.ApplicationPath).Replace("/", "\\")
-> d:\xxx\zzz\aspgod.com\httpdocs

—–

If I use this script on localhost the the result is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Request.Url.AbsoluteUri  //  http://localhost:1904/aspgod/test/mappath/Default.aspx
Request.Url.AbsolutePath  //  /test/mappath/Default.aspx
Request.ApplicationPath  //  /aspgod
 
my project is in this path E:\--- Other Project ---\aspgod\program\aspgod
Server.MapPath("") // E:\--- Other Project ---\aspgod\program\aspgod\test\mapath
Server.MapPath("Default.aspx")
// E:\--- Other Project ---\aspgod\program\aspgod\test\mapath\Default.aspx
 
if you want to get the root path and root url.
You must enter this script below:
 
Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath,string.Empty) + Request.ApplicationPath
//  http://localhost:1904/aspgod
Server.MapPath(this.Request.ApplicationPath).Replace("/", "\\")
// E:\--- Other Project ---\aspgod\program\aspgod

GridView RowDataBound Get DataSource By Row

When using RowDataBound (the GridView’s method) and want to get the data by row. Because RowDataBound work on row by row when initial GridView. How to get data from datasource in method RowDataBound ? Use Eval() method will help you

protected void gvView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
_SumTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, “Total”));
}
}

// “Total” is a data field in database
// _SumTotal is a global valuable on this page

In this example you can get all total summary in gridview datasource.

Connect Sql Server using windows authentication

When using Sql Server Authentication to connect database on other server the connection string is :

Data Source=serverIP;Initial Catalog=databaseName;User ID=UserName;Password=Pass

If you want to connect to your own computer with windows authentication. The same connection string doesn’t work on that way. Try new connection string to :

Server=localhost;Database=databaseName;Trusted_Connection=Yes;

Let’s try and told me the result of this problem. :D

PS : ASP.NET connecting to SQL Server