Ask Questions & Get Answers at ibibo sawaal

113

Rank

8147

1692

26

34

What is the easiest way to fetch a Web page in .NET? ?

Asked by Lucky Singh in Computers & Technology at   8:42 AM on December 03, 2008

Sudipta Deb's Answer

The System.Net.WebRequest class offers a simple, easy-to-use method of retrieving Web pages. The WebRequest class features many options including asynchronous downloading and numerous properties for both the request and response.
For basic, simplified applications, the System.Net.WebClient class can be a viable alternative to the WebRequest class.
The following example illustrates a basic application of the System.Net.WebRequest class:
using System.IO;
using System.Net;
using System.Text;

...

string url = "http://en.csharp-online.net/" ;

WebRequest request = WebRequest.Create (url);

// For HTTP, cast the request to HttpWebRequest
// allowing setting more properties, e.g. User-Agent.
// An HTTP response can be cast to HttpWebResponse.

using (WebResponse response = request.GetResponse())
{
// Ensure that the correct encoding is used.
// Check the response for the Web server encoding.
// For binary content, use a stream directly rather
// than wrapping it with StreamReader.

using (StreamReader reader = new StreamReader
(resp.GetResponseStream(), Encoding.UTF8))
{
string content = reader.ReadToEnd();
// process the content
}
}

Source: http://en.csharp-online.net/CS harp_FAQ:_What_is_the_easiest_ way_to_fetch_a_Web_page_in_.NE T

Answered at 9:07 AM on December 03, 2008

Read all answers

How create an instance of a type using only its name? ?

Asked by Lucky Singh in Computers & Technology at   8:42 AM on December 03, 2008

Sudipta Deb's Answer

Firstly, get a Type reference for the type. If the type to be instantiated is in either the current assembly or mscorlib, simply use Type.GetType(name). If the type is in a different assembly, there are two options: either call Type.GetType passing in the full type name including assembly information; or, find or load the assembly then call Assembly.GetType(name) using the assembly reference.
With the Type reference, use either Activator.CreateInstance(type) to create an instance; or, call Type.GetConstructor to get a specific constructor which can then be used instantiate by calling Invoke.

Source: http://en.csharp-online.net/CS harp_FAQ:_How_create_an_instan ce_of_a_type_using_only_its_na me

Answered at 9:06 AM on December 03, 2008

Read all answers

What compression and zipping capabilities does .NET have? ?

Asked by Lucky Singh in Computers & Technology at   8:41 AM on December 03, 2008

Sudipta Deb's Answer

As of version 1.1, the .NET Framework did not contain any general purpose compression libraries. However, there are numerous third-party libraries available. SharpZipLib is a popular, free, open source compression library which can be used in commercial applications.
As of version 2.0, the .NET Framework contains a few compression routines. They can be found in the System.IO.Compression namespace. However, it does not provide support for .zip files proper.
Source: http://en.csharp-online.net/CS harp_FAQ:_What_compression_zip ping_capabilities_has_.NET

Answered at 8:59 AM on December 03, 2008

Read all answers

What is the difference between Close and Dispose?

Asked by Lucky Singh in Computers & Technology at   8:48 AM on December 03, 2008

Sudipta Deb's Answer

The basic difference between Close() and Dispose() is, when a Close() method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used. Where as Dispose() method permanently removes any resource ((un)managed) from memory for cleanup and the resource no longer exists for any further processing.

Example showing difference between Close() and Dispose() Method:


using System;
using System.Data;
using System.Data.SqlClient;
public class Test
{
private string connString = "Data Source=COMP3;Initial Catalog=Northwind;User Id=sa;Password=pass";
private SqlConnection connection;
public Test()
{
connection = new SqlConnection(connString);
}
priv ate static void Main()
{
Test t = new Test();
t.ConnectionStatus();
Con sole.ReadLine();
}
public void ConnectionStatus()
{
try
{
if(c onnection.State == ConnectionState.Closed)
{
connect ion.Open();
Console.WriteLine("Con nection opened..");
}

if(connection.Sta te == ConnectionState.Open)
{
connectio n.Close();
Console.WriteLine("Conn ection closed..");
}
// connection.Dispose();

if(connect ion.State == ConnectionState.Closed)
{
connect ion.Open();
Console.WriteLine("Con nection again opened..");
}
}
catch(SqlExcepti on ex)
{
Console.WriteLine(ex.Messag e+"\n"+ex.StackTrace);
}
catch(Ex ception ey)
{
Console.WriteLine(ey.Messag e+"\n"+ey.StackTrace);
}
finally
{
Console.WriteLine("Connection closed and disposed..");
connection.Dispose() ;
}
}
}

In the above example if you uncomment the "connection.Dispose()" method and execute, you will get an exception as, "The ConnectionString property has not been initialized.".This is the difference between Close() and Dispose().

Answered at 8:53 AM on December 03, 2008

Read all answers

What is the difference between Convert.ToInt32(string) and Int32.Parse(string)? ?

Asked by Lucky Singh in Computers & Technology at   8:42 AM on December 03, 2008

Sudipta Deb's Answer

Basically the Convert class makes it easier to convert between all the base types.

The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException.

It is really a matter of choice whichever you use.

Answered at 8:58 AM on December 03, 2008

Read all answers

How convert a string type to an int type? ?

Asked by Lucky Singh in Computers & Technology at   8:42 AM on December 03, 2008

Sudipta Deb's Answer

Please find the below program:
#include <iostream>
#include <cstring>
using namespace std;

class StringClass {
char str[80];
int len;
public:
StringClass(char *s) {
strcpy(str, s);
len = strlen(s);
}
operator char *() {
return str;
}
operator int() {
return len;
}
};

int main()
{
StringClass s("Conversion functions are convenient.");
char *p;
int l;

l = s; // convert s to integer - which is length of string
p = s; // convert s to char * - which is pointer to string

cout << "The string:\n";
cout << p << "\nis " << l << " chars long.\n";

return 0;
}

Answered at 9:02 AM on December 03, 2008

Read all answers

How escape text in SQL statements?

Asked by Lucky Singh in Computers & Technology at   8:47 AM on December 03, 2008

Sudipta Deb's Answer

The best answer answer is not to put escaped data into SQL statements in the first place: Use parameters instead. Virtually, all database providers provide a method of specifying parameters in SQL statements.
The parameters may be either named—e.g. SELECT NAME FROM PEOPLE WHERE ID=@ID—or positional—e.g. SELECT NAME FROM PEOPLE WHERE ID=?. Whereas some providers support positional parameters only, others support named parameters only, and, still others support both.
Parameters are a mechanism for creating a SQL statement which can accept variable values. Use a SqlCommand—or the equivalent for the database in use—to specify the query text, the command type—stored procedure, text, etc.—and the parameters themselves. Create the command once with parameters; and, from then on, reuse the command by setting the desired parameter values for each request. When using a DataAdapter to update the database, parameter values for the relevant command—delete, insert, or update—are filled in automatically.
Parameters enable writing SQL without having to escape values or format dates, times, etc. And, SQL injection attacks are not a concern; because, parameter values are not be used as SQL itself. Further, parameters are simpler for the database software to handle. For example, the database can cache the query for faster execution with the actual parameter values provided.
Source: http://en.csharp-online.net/CSharp_ FAQ:_How_escape_text_in_SQL_stateme nts

Answered at 8:51 AM on December 03, 2008

Read all answers

Virtual Execution System please explain???

Asked by arun nair in Computers & Technology at   12:35 AM on December 01, 2008

Sudipta Deb's Answer

The Virtual Execution System(VES) provides an environment for executing managed code. It provides direct support for a set of built-in data types, defines a hypothetical machine with an associated machine model and state, a set of control flow constructs, and an exception handling model. To a large extent, the purpose of the VES is to provide the support required to execute the Common Intermediate Language instruction set.
Source: http://en.wikipedia.org/wiki/V irtual_Execution_System

Answered at 9:24 AM on December 01, 2008

Read all answers

How express dates and times in SQL statements?

Asked by Lucky Singh in Computers & Technology at   8:48 AM on December 03, 2008

Sudipta Deb's Answer

The best answer answer is not to put escaped data into SQL statements in the first place: Use parameters instead. Virtually, all database providers provide a method of specifying parameters in SQL statements.
The parameters may be either named—e.g. SELECT NAME FROM PEOPLE WHERE ID=@ID—or positional—e.g. SELECT NAME FROM PEOPLE WHERE ID=?. Whereas some providers support positional parameters only, others support named parameters only, and, still others support both.
Parameters are a mechanism for creating a SQL statement which can accept variable values. Use a SqlCommand—or the equivalent for the database in use—to specify the query text, the command type—stored procedure, text, etc.—and the parameters themselves. Create the command once with parameters; and, from then on, reuse the command by setting the desired parameter values for each request. When using a DataAdapter to update the database, parameter values for the relevant command—delete, insert, or update—are filled in automatically.
Parameters enable writing SQL without having to escape values or format dates, times, etc. And, SQL injection attacks are not a concern; because, parameter values are not be used as SQL itself. Further, parameters are simpler for the database software to handle. For example, the database can cache the query for faster execution with the actual parameter values provided.
Source: http://en.csharp-online.net/CSharp_ FAQ:_How_express_dates_and_times_in _SQL_statements

Answered at 8:52 AM on December 03, 2008

Read all answers

How monitor changes to Clipboard contents?

Asked by Lucky Singh in Computers & Technology at   8:48 AM on December 03, 2008

Sudipta Deb's Answer

Call the SetClipboardViewer function in the initialization code passing in the form handle. Then, the form will receive WM_DRAWCLIPBOARD messages that can be handled in WndProc.
using System.Runtime.InteropServices ;

...

[DllImport("user32.dll")]

private const int WM_DRAWCLIPBOARD = 776;

public static extern int SetClipboardViewer (int windowHandle);

...

// within the initialization code

SetClipboardViewer (this.Handle.ToInt32());

protected override void WndProc (ref Message message)
{
base.WndProc (ref message); // Process the message
if (message.Msg == WM_DRAWCLIPBOARD)
{
// act on Clipboard changes
}

Answered at 8:50 AM on December 03, 2008

Read all answers

Editor's Pick

Categories

sawaal signature
sawaal free visiting card