选择逻辑问题 [英] Selection logic problem

查看:102
本文介绍了选择逻辑问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试检查数据库中的库存数量。如果在调度webform中输入的quatinty小于库存,则应继续发送。否则我应该看到错误信息,发货数量超过库存并终止流程。





Hi guys,

I am trying to check for inventory quantity in database. If quatinty entered in dispatch webform is less than inventory, the dispatch should continue. else i should see error message that dispatch quantity is more than inventory and terminate the process.


int SelectedDPItem;
int InventoryQTY;
int DinventoryQTY;

SqlConnection Connection = new SqlConnection("Data Source=localhost;Initial Catalog=warehousedb;Integrated Security=True ");
SqlDataAdapter INVadapter = new SqlDataAdapter("Select ItemID, ItemName, Quantity FROM inventory ", Connection);

SelectedDPItem = int.Parse(DropDownListItem_D.SelectedValue.ToString());
// Open Connection
Connection.Open();
SqlCommand Inventorychkcommand = new SqlCommand(("Select Quantity FROM inventory " + ("WHERE (ItemName LIKE \'"
+ (DropDownListItem_D.Text + "\')"))), Connection);
InventoryQTY = Convert.ToInt32(Inventorychkcommand.ExecuteScalar());
DinventoryQTY = Convert.ToInt32(txtQuantity_D.Text);



if (InventoryQTY < DinventoryQTY)
{

	{

		ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>alert('sorry , Stock Balance is less than requested quantity.')</script>");
	}
}


else
{

	{
		// procedure of inserting the record
		AddNewRecordRowToGrid();

		txtwaybill_D.Enabled = false;
		DropDownListTransp.Enabled = false;
		txtTruckNo_D.Enabled = false;
		txtDrivername_D.Enabled = false;
		DropDownListEmployee_D.Enabled = false;
		btnNew.Enabled = false;
		btnPostDispatch.Enabled = true;

		DropDownListItem_D.Enabled = false;
		txtDispatchDate.Enabled = false;
		txtDestn.Enabled = false;
		txtQuantity_D.Enabled = false;
		txtDestn.Enabled = false;
	}





我的尝试:



我正在尝试检查数据库中的库存数量。如果在调度webform中输入的quatinty小于库存,则应继续发送。否则我应该看到错误信息,发货数量超过库存并终止流程。







当我尝试调度数量少于库存时,错误仍会弹出,程序不会继续执行调度。我不想要这个。



我正在实现addbutton中的代码,该代码应该将项目插入gridview(仅当数量超过库存时)。



What I have tried:

I am trying to check for inventory quantity in database. If quatinty entered in dispatch webform is less than inventory, the dispatch should continue. else i should see error message that dispatch quantity is more than inventory and terminate the process.



When i try dispatching quantity less than inventory, the error still pops up and the program does not continue to excecute dispatch. i do not want this.

I am implementing the code from addbutton that should insert item to gridview (only if quantity is more than inventory).

推荐答案

几件事:

1)不要使用虚假括号 - 它们只会让你更难阅读并弄清楚发生了什么:

Couple of things:
1) Don't use "spurious" brackets - they only make it harder to read and work out what is going on:
if (InventoryQTY < DinventoryQTY)
{
 
{
 
ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>alert('sorry , Stock Balance is less than requested quantity.')</script>");
}
}
 

else
{
 
{

只需尝试一组:

Try just the one set each:

if (InventoryQTY < DinventoryQTY)
   {
   ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>alert('sorry , Stock Balance is less than requested quantity.')</script>");
   }
else
   {





2)不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。你是非常安全的,因为它;下拉列表,但无论如何这是一个坏主意。



3)LIKE是一个通配符匹配系统,如* .txt在文件名中 - 仅对于SQL,通配符不是'*',它是'%'。因此,除非您的ItemName与您提供的值完全匹配,否则您将无法从DB获得任何好的响应。如果你真的想要一个通配符匹配,那么ExecuteScalar是一个糟糕的选择,因为通配符通常不会;返回一个值!



我建议你从使用调试器确切地计算出你传递给SQL的内容,以及你得到的确切内容:因为这是所有数据依赖的,我们不能为你做到这一点!



2) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. You are fairly safe here since it;s a drop down list, but it's a bad idea anyway.

3) LIKE is a "wildcard" matching system, like "*.txt" in filenames - only for SQL the wildcard character is not '*' it's '%'. So unless your ItemName exactly matches the value you supply you won't get any good responses from the DB. If you actually want a wildcard match, then ExecuteScalar is a poor choice, as wildcards generally don;t return a single value!

I'd suggest that you start by using the debugger to work out exactly what you are passing to SQL, and exactly what you get back: since this is all data dependant, we can't do that for you!


这篇关于选择逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆