Wednesday, March 21, 2012

Please can someone point out whats wrong with my code

Hi,

I have code on a page to update one table and insert info into another, but I cant make it work. I am new to coding and i think there are many mistakes. Please can someone pick out a few that need to be changed for the page to work.

Code:

private bool ExecuteUpdate(int quantity)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "ConnectionString";

con.Open();

SqlCommand command = new SqlCommand();
command.Connection = con;
TextBox TextBox1 = (TextBox)FormView1.FindControl("TextBox1");
Label labname = (Label)FormView1.FindControl("Label3");
Label labid = (Label)FormView1.FindControl("Label13");

command.CommandText = "UPDATE Items SET Quantityavailable WHERE productID='@.productID' = " + TextBox1.Text +

command.ExecuteNonQuery();
return true;
con.Close();
}

private bool ExecuteInsert(String quantity)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "ConnectionString";

con.Open();

SqlCommand command = new SqlCommand();
command.Connection = con;
TextBox TextBox1 = (TextBox)FormView1.FindControl("TextBox1");
Label labname = (Label)FormView1.FindControl("Label3");
Label labid = (Label)FormView1.FindControl("Label13");

command.CommandText = "INSERT Transactions SET Usersname = @.UserName" +
"; INSERT Transactions SET Itemid WHERE productID='@.productID' = @.productID; INSERT Transactions SET itemname = @.Itemsname" +
"; INSERT Transactions SET Date WHERE productID='@.productID' = " + DateTime.Now.ToString() +
"; INSERT Transactions SET Qty WHERE productID='@.productID' = " + TextBox1.Text;
command.Parameters.Add("@.UserName", System.Web.HttpContext.Current.User.Identity.Name);
command.Parameters.Add("@.Itemsname", labname.Text);
command.Parameters.Add("@.productID", labid.Text);
command.ExecuteNonQuery();
return true;
con.Close();
}

protected void Button2_Click(object sender, EventArgs e)
{
TextBox TextBox1 = FormView1.FindControl("TextBox1") as TextBox;
ExecuteUpdate(Int32.Parse(TextBox1.Text) );
ExecuteInsert(Int32.Parse);
}

Current error message is:

The best overloaded method match for 'detailproview.ExecuteInsert(string)' has some invalid arguments

to

Line 74: ExecuteInsert(Int32.Parse);
 
Thanks if someone can help!
Jon 

Hi,

The best way for you to see why it is not working is to debug your code and check the values in the object using Immediate window or pointing the mounse on the objects after value is assigned.

Maybe you are not getting the id that you are using in the WHERE lause of your update statement. Make sure you are getting valid id and that it exists in database.

Finally you are calling return true before closing the connection, first close the connection than return a value.

|||


Int32.Parse is a method and you need to pass in an argument to it, the one string which you want to conevrt to an int, like you are doing it here:

ExecuteUpdate(Int32.Parse(TextBox1.Text) );

Hope this helps,

Vivek

|||

Hi,

Thanks both great to help me iron out bugs.

I will put up a new post with the latest set of error messages!

Cheers

Jon

No comments:

Post a Comment