0

I have a little problem with selecting max ID from my database. This is my code:

string checkcat = "SELECT MAX(PRODUCT_ID) FROM CMS_PRODUKTY WHERE (CATEGORY_ID = CATEGORY_ID) AND (CLIENT_ID = @CLIENT_ID)";
SqlCommand cmd2 = new SqlCommand(checkcat, con);
cmd2.Parameters.Add("@CATEGORY_ID", System.Data.SqlDbType.Int).Value = kategoria;
cmd2.Parameters.Add("@CLIENT_ID", System.Data.SqlDbType.Int).Value = HiddenField1.Value;
con.Open();
int noweid = Convert.ToInt32(cmd2.ExecuteScalar());
con.Close();

The point is the new int "noweid" should be 1 or higher - depend on product_id inside table, but it's returning 0. I don't have any idea why... The other variables - kategoria and HiddenField1.Value are correct.

Any ideas what did I do wrong?

4

1 回答 1

2

你有

WHERE (CATEGORY_ID = CATEGORY_ID)

但你想要

WHERE (CATEGORY_ID = @CATEGORY_ID)

您还应该将字符串解析为int

cmd2.Parameters.Add("@CLIENT_ID", System.Data.SqlDbType.Int).Value = int.Parse(HiddenField1.Value);

也许kategoria也是一个string,然后将其解析为int

于 2015-09-28T11:44:15.743 回答