How can i rectify these errors ?

Posted by Sudheep.grandhe under C# on 10/31/2011 | Points: 10 | Views : 2770 | Status : [Member] | Replies : 3
Hi,

In my coding it shows the following errors.i am underlying those errors.please find.please do the needful.

Error 3:-Expected class, delegate, enum, interface, or struct
public void netamount()
Error 4 Expected class, delegate, enum, interface, or struct
SqlConnection con = new SqlConnection ("data source=mars-104;initial catalog=marsweb.imageimpacts;integrated security=true");
Error 5 Expected class, delegate, enum, interface, or struct
SqlCommand cmd = new SqlCommand ("select sum(Amount)from tbl_Report1 where OrderNo=@InvoiceId", con);
Error 6 Expected class, delegate, enum, interface, or struct
SqlDataAdapter da = new SqlDataAdapter (cmd);
Error 7 Expected class, delegate, enum, interface, or struct
DataTable dt = new DataTable ();
Error 8 Identifier expected
dt.Rows[0] [0].ToString();
public void netamount()
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb.imageimpacts;integrated security=true");
SqlCommand cmd = new SqlCommand("select sum(Amount)from tbl_Report1 where OrderNo=@InvoiceId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@invoiceid");
con.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
txt_netamount.Text = dt.Rows[0][0].ToString();

}

Best,
Sudheep.



Responses

Posted by: SheoNarayan on: 10/31/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Sudheep,

When you have a SQL statement that returns some data, you can't use ExecuteNonQuery() method of SqlCommand.

Read this article to know more on how to retrieve records from database in ASP.NET http://www.dotnetfunda.com/articles/article1513-how-to-retrieve-data-from-the-database-in-aspnet.aspx

In this case you are retrieving the count, so you should use either .ExecuteScalar() or above link approach. If you choose to use ExecuteScalar() method read http://www.dotnetfunda.com/articles/article1147-how-to-use-executescalar-method.aspx.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 11/2/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

presently iam developing windows applications.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: NTurnbullJr on: 11/5/2011 [Member] Starter | Points: 25

Up
0
Down
It appears you are missing a { after public void netamount() .
Also, although it is not shown in your example, it seems you are missing the class definition that comes before public void netamount() ?
Lastly, in
SqlCommand cmd = new SqlCommand("select sum(Amount)from tbl_Report1 where OrderNo=@InvoiceId", con);
there needs to be a space between sum(Amount) and from

Best regards,
Norm

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response