Error while running asp.net

S

sha2kk3

While running this code "Invalid token 'try' in class, struct, or
interface member declaration" error is comming. Please advice me......
I am new to asp.net.

try
{
string connString = "Server=server04;Database=Northwind;Integrated
Security=SSPI";
string sql = "SELECT * FROM EmployeeDetails";

SqlConnection conn = new SqlConnection( connString );
SqlCommand command = new SqlCommand( sql, conn );

conn.Open();
SqlDataReader reader = command.ExecuteReader();
this.dgEmployeedetails.DataSource = reader;
conn.Close();
// this.dgEmployeedetails.DataBind();
}
catch ( Exception ex )
{
MessageBox.Show (ex.Message);
}
finally
{
conn.Close();
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You have to place this code inside a method, apparently you have it below
the class definition
 
G

Guest

Ignacio is right, you need to place this code inside a method. For ASP.NET
you would normally load page in Page_Load event. But your code under catch
block will not work because MessageBox is a Windows Forms class not available
under ASP.NET. I would follow some ASP.NET quickstart tutorials available on
gotdotnet. Here's the link: http://samples.gotdotnet.com/quickstart/aspplus/
 
N

noblespirit.an

string connString = "Server=server04;Database=Northwind;Integrated
Security=SSPI";
string sql = "SELECT * FROM EmployeeDetails";

SqlConnection conn = new SqlConnection( connString );
try
{

SqlCommand command = new SqlCommand( sql, conn );

conn.Open();
SqlDataReader reader = command.ExecuteReader();
this.dgEmployeedetails.DataSource = reader;
conn.Close();
//
this.dgEmployeedetails.DataBind();
}
catch ( Exception ex )
{
MessageBox.Show (ex.Message);
}
finally
{
conn.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top