Archive for the ‘Asp.net’ Category

Remove white spaces in a string c#

By using trim() we can remove white space characters from the beginning and end of a string.But to remove white space characters in middle we cannot use trim() method.

we can easily remove the white spaces/empty space in a string by replace method

string strSample = “Test with white space”;
strSample = strSample.Replace(” “, “”);

Result : strSample = “Testwithwhitespace”

Popularity: 6% [?]

How to open Immediate Window In VS 2005- Shortcuts

The Immediate window is used to debug and evaluate expressions, execute statements, print variable values etc.The Immediate window also supports IntelliSense.There are different methods to open Immediate window.

Method 1 : -
We can open Immediate Window from debug menu.

Debug->Windows->Immediate Window

immediate

Some times Immediate Window menu would be missing.If it seems to be missing for you then you can get the Immediate Window menu by going to Tools->Customize and then select the Commands tab, choose for Debug commands and drag Immediate onto the toolbar.Read more about this issue here.

Method 2 : -

Use the Keyboard Shortcut keys Ctrl+Alt+I

Method 3 : -
We can also open Immediate Window through VS Command window.Just type “immed” in the prompt and hit enter Immediate Window will open.

commimmed

For more details about Immediate Window go to MSDN Link

Popularity: 13% [?]

Steps to Create Setup and Deployment Project in Dot Net VS 2008

Step 1
Create your own windows application. Create a new Windows application project in C# and named it as Sample.

d01

Step 2

Design your own application. Here we have a simple login form for example.

d02
Step 3
After completing the design and coding, build the solution of the project in release mode.

d03

Step 4
Check the Release folder for the file “ProjectName.exe”. Here in this example we have the project name as sample so we can find a file with the name Sample.exe. Double click the executable file and check the example.

d04

Step 5
Create a Deployment Project. Select the “Other Project Types” -> “Setup and Deployment” -> “Setup project”. Here we have the setup project for example as “SampleSetup”.

d05

Step 6
Add the Sample.exe project application file inside the “Application Folder”.

d06

Step 7

To make a shortcut for the project right click “File System on Target Machine” and create shortcut of the application. Here in this example the project shortcut is created in program files folder.

d07

Step 8
Create the shortcut of the application.

d08

Step 9
Rename the shortcut of the application.

d09

Step 10
Move the Shortcut file to specified target. Note if you need another shortcut for some other target also create use same steps.

d10

Step 11
Now build the solution in release mode.

d11

Step 12
The setup file created in release folder of the project specified path.

d12

Step 13
Run the setup, step the path to extract.

d13

d14

d15

Step 14
The SampleSetup project is extracted and shortcuts are created. Now run your application.

d16

Popularity: 100% [?]

Get Clients IP Address Using ASP.NET

We can get the clients IP address using the Request object’s(The Request object retrieves the values that the client browser passed to the server during an HTTP request. ) property Request.ServerVariables Collection.
Request.ServerVariables Collection gets the values of predetermined environment variables.

“REMOTE_ADDR” gets the IP address of the remote host making the request. 

string ipaddr;

ipaddr = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

here the string “ipaddr” stores the clients ip.

List Of other Request.ServerVariables Object

Variable Description
AUTH_TYPE The authentication method that the server uses to validate users when they attempt to access a protected script.
CONTENT_LENGTH The length of the content as given by the client.
CONTENT_TYPE The data type of the content. Used with queries that have attached information, such as the HTTP queries POST and PUT.
GATEWAY_INTERFACE The revision of the CGI specification used by the server. Format: CGI/revision.

HTTP_<HeaderName> The value stored in the header HeaderName. Any header other than those
listed in this table must be prefixed by “HTTP_” in order for the
ServerVariables collection to retrieve its value.

Note: The server interprets any underscore (_) characters in
HeaderName as dashes in the actual header. For example if you specify
HTTP_MY_HEADER, the server searches for a header sent as MY-HEADER.

LOGON_USER The Windows NT® account that the user is logged into.
PATH_INFO Extra path information as given by the client. You can access scripts
by using their virtual path and the PATH_INFO server variable. If this
information comes from a URL it is decoded by the server before it is
passed to the CGI script.
PATH_TRANSLATED A translated version of PATH_INFO that takes the path and performs any necessary virtual-to-physical mapping.
QUERY_STRING Query information stored in the string following the question mark (?) in the HTTP request.
REMOTE_ADDR The IP address of the remote host making the request.
REMOTE_HOST The name of the host making the request. If the server does not have
this information, it will set REMOTE_ADDR and leave this empty.
REQUEST_METHOD The method used to make the request. For HTTP, this is GET, HEAD, POST, and so on.
SCRIPT_MAP Gives the base portion of the URL.
SCRIPT_NAME A virtual path to the script being executed. This is used for self-referencing URLs.
SERVER_NAME The server’s host name, DNS alias, or IP address as it would appear in self-referencing URLs.
SERVER_PORT The port number to which the request was sent.
SERVER_PORT_SECURE A string that contains either 0 or 1. If the request is being handled
on the secure port, then this will be 1. Otherwise, it will be 0.
SERVER_PROTOCOL The name and revision of the request information protocol. Format: protocol/revision.
SERVER_SOFTWARE The name and version of the server software answering the request (and running the gateway). Format: name/version.
URL Gives the base portion of the URL.

Popularity: 31% [?]

How to Upload File in Asp.net

Here in this example we browse a file from our pc and upload that file to some other location in our pc.

Default.aspx

<input type=file id=File1 name=File1 runat=”server”>
<input type=”submit” id=”Submit1″ value=”Upload” runat=”server” name=”Submit1″>

Defalut.aspx.cs

protected void Submit1_ServerClick(object sender, EventArgs e)
{
if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
{
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);

//Create Data Folder within the Location u Specified
string SaveLocation = “D:\UploadedFiles” + “\” + fn;

//File will be Uploaded to the Specified Location
try
{
File1.PostedFile.SaveAs(SaveLocation);
Response.Write(“The file has been uploaded.”);
}
catch ( Exception ex )
{
Response.Write(“Error: ” + ex.Message);
}
}
else
{
Response.Write(“Please select a file to upload.”);
}
}

You can give the specified location to upload in SaveLocation string.

Download

Popularity: 2% [?]

Simple Add,Edit,Retrieve and Delete in Asp.net

Here is a example application of Add,Edit,Retrieve and Delete operations using Asp.net. Download Creating a table employeeinfo for proceeding the operations. Sql Table Creation

CREATE TABLE employeeinfo ( empid INT NOT NULL PRIMARY KEY, empname VARCHAR(50), empaddress VARCHAR(200), empsalary INT )

Declaring Connection String and Variables

Namespace : using System.Data.SqlClient;  string QryStr = ""; SqlConnection con = new SqlConnection("Server=servername;Integrated Security=true;Database=employee"); SqlCommand cmd; SqlDataReader dr;

In this example i have used windows authentication.You have to change the connection string according your specification. Adding Records

QryStr = "INSERT INTO employeeinfo VALUES('" +txtEmpId.Text + "','" + txtEmpName.Text + "','" + txtEmpAdd.Text + "','" + txtEmpSal.Text + "')"; con.Open(); cmd = new SqlCommand(QryStr, con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close();

Updating Records

QryStr = "UPDATE employeeinfo SET empname = '"+txtEmpName.Text+"',empaddress = '"+txtEmpAdd.Text+"',empsalary = '"+txtEmpSal.Text+"' WHERE empid = '"+txtEmpId.Text+"'"; con.Open(); cmd = new SqlCommand(QryStr, con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close();

Retrieve Records to textboxes Records will be Retrieved from the database and displayed in the text boxes when text change of employee id is fired.

QryStr = "SELECT * FROM employeeinfo WHERE empid = '" + txtEmpId.Text + "'";  con.Open(); cmd = new SqlCommand(QryStr, con); dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows) { txtEmpName.Text = dr["empname"].ToString(); txtEmpAdd.Text = dr["empaddress"].ToString(); txtEmpSal.Text = dr["empsalary"].ToString(); } else { txtEmpName.Focus(); } con.Close();

Deleting Records

QryStr = "DELETE FROM employeeinfo WHERE empid = '" + txtEmpId.Text + "'"; con.Open(); cmd = new SqlCommand(QryStr, con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close();

Popularity: 2% [?]

What is ViewState ?

ASP.NET view state is the technique used by an ASP.NET Web page to persist
changes to the state of a Web Form across postbacks.That is when a form is submitted in ASP .NET, the form reappears in the browser window together with all form values.

The ViewState indicates the status of the page when submitted to the server. The status is defined through a hidden field placed on each page with a <form runat=”server”> control.

Hidden field placed on each page would be something as this
< input type=”hidden” name=”__VIEWSTATE” value=”CEbzzzEnmmz+Bc8IDFlnpgCLJ/HB00.” >
Maintaining the ViewState is the default setting for ASP.NET Web Forms. If you want to NOT maintain the ViewState, include the directive l%@ Page EnableViewState=”false” %> at the top of an .aspx page or add the attribute EnableViewState=”false” to any control.

Popularity: 1% [?]

What is Tracing?

Tracing is a refined process for outputting page-level information.When tracing is enabled you automatically get information on the ASP.NET Web page. Information like:

Request Details – Session Id; Request time, type, and encoding; status code, etc.
Trace Information – Page-level ASP.NET messages that you specify via Trace.Write and Trace.Warn.
Control Tree – A listing of the Web controls on theASP.NET Web page,and how they relate to one another.
Cookies Collection – A listing of all of the cookies.
Headers Collection
– A listing of all of the HTTP headers.
Server Variables – A listing of all of the server variables.

Trace statements are processed and displayed only when tracing is enabled. You can control whether tracing is displayed to a page, to the trace viewer, or both.

Popularity: 1% [?]

What Is ASP.Net?

ASP.NET is a web application framework built on the common language runtime used to build dynamic web sites, web applications and XML web services.ASP.NET is the next generation ASP, but it’s not an upgraded version of ASP.

ASP.NET is a program that runs inside IIS and is a part of Microsoft’s .NET platform. ASP.NET allows you to use a full featured programming language such as C# (pronounced C-Sharp) or VB.NET to build web applications easily. An ASP.NET file has the file extension “.aspx” .

ASP.NET makes it simple to use XML for data storage, configuration and manipulation. The tools which are built into ASP.NET for working with XML are very easy to use.
Best of all, ASP.NET pages work in all browsers including Netscape, Opera, AOL, and Internet Explorer.

Visual Studio 2005 adds the productivity of Visual Basic-style development to the Web. Now you can visually design ASP.NET Web Forms using familiar drag-drop-double-click techniques, and enjoy full-fledged code support including statement completion and color-coding. VS.NET also provides integrated support for debugging and deploying ASP.NET Web applications.

Popularity: 1% [?]

How to open an application, file Using Shell execute in .net

We can execute any application(any files,notepad,calculator and etc) using C#.net by
System.Diagnostics.Process.Start.

Coding :

private void bt_ShellExecute_Click(object sender, EventArgs e)
{
string strpath = “C:\\sample.xls”;

System.Diagnostics.Process.Start(strpath);
}

Popularity: 4% [?]

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars