如何在数据库中检查所选的下拉列值是否存在 [英] How to check selected dropdown value is exists or not in database

查看:82
本文介绍了如何在数据库中检查所选的下拉列值是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据库中检查选定的下拉值

当我们从dropdaown中选择商品时,此值不在此仓库的数据库中,它不会显示任何消息。



我尝试过:



< pre> protected void ddlCommodity_SelectedIndexChanged(object sender,EventArgs e)
{
Int64 Cmdty = Convert.ToInt64(ddlCommodity.SelectedItem.Value);
if((Convert.ToInt32(ddlWarehouse.SelectedItem.Value))!= 0)
{
// int InsuranceId = Convert.ToInt32(grd.DataKeys [e.RowIndex] .Values [0]);
string constr = WebConfigurationManager.ConnectionStrings [WarehouseWebsiteString]。ConnectionString;

使用(SqlConnection con = new SqlConnection(constr))
{
using(SqlCommand cmd = new SqlCommand(select tmodityId from tblCommodityonWarehouse WHERE WarehouseId = @WarehouseId))
{
cmd.Parameters.AddWithValue(@ CommodityId,Cmdty);
cmd.Parameters.AddWithValue(@ WarehouseId,ddlWarehouse.SelectedIndex);
cmd.Connection = con;
con.Open();
int k = cmd.ExecuteNonQuery();
if(k> 0)
{
string popupScript =$ .prompt('此商品不可用。请使用其他。');;
ScriptManager.RegisterClientScriptBlock(this.Page,this.Page.GetType(),clientScript,popupScript,true);
ddlCommodity.SelectedIndex = -1;
}
// else
// {

//}
con.Close();
}

}

解决方案

.prompt('此商品不可用。请使用另一个。');;
ScriptManager.RegisterClientScriptBlock(this.Page,this.Page.GetType(),clientScript,popupScript,true);
ddlCommodity.SelectedIndex = -1;
}
// else
// {

//}
con.Close();
}

}


你是否意识到

 int k = cmd.ExecuteNonQuery(); 

不返回记录集?它用于INSERT,UPDATE和DELETE,CREATE,ALTER等。



SELECT 语句通常是与

 reader = cmd.ExecuteReader(); 

一起使用,你需要创建一个dataReader。



How to check selected dropdown value is available in database
When we select commodity from dropdaown and this value is not in database with this warehouse it does not show any message .

What I have tried:

<pre>protected void ddlCommodity_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        Int64 Cmdty = Convert.ToInt64(ddlCommodity.SelectedItem.Value);   
        if ((Convert.ToInt32(ddlWarehouse.SelectedItem.Value)) != 0)  
        {  
            //int InsuranceId = Convert.ToInt32(grd.DataKeys[e.RowIndex].Values[0]);  
            string constr = WebConfigurationManager.ConnectionStrings["WarehouseWebsiteString"].ConnectionString;  
  
            using (SqlConnection con = new SqlConnection(constr))  
            {  
                using (SqlCommand cmd = new SqlCommand("Select CommodityId from tblCommodityonWarehouse WHERE WarehouseId = @WarehouseId"))  
                {  
                    cmd.Parameters.AddWithValue("@CommodityId", Cmdty);  
                    cmd.Parameters.AddWithValue("@WarehouseId", ddlWarehouse.SelectedIndex);  
                    cmd.Connection = con;  
                    con.Open();  
                    int k = cmd.ExecuteNonQuery();  
                    if (k > 0)  
                    {  
                        string popupScript = "$.prompt('This Commodity is Not Available. Please use another.');";  
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "clientScript", popupScript, true);  
                        ddlCommodity.SelectedIndex = -1;  
                    }  
                    //else  
                    //{  
  
                    //}  
                    con.Close();  
                }  
  
            }  

解决方案

.prompt('This Commodity is Not Available. Please use another.');"; ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "clientScript", popupScript, true); ddlCommodity.SelectedIndex = -1; } //else //{ //} con.Close(); } }


Do you realize that

int k = cmd.ExecuteNonQuery();

does not return a record set ? It's used for INSERT, UPDATE, and DELETE, CREATE, ALTER, and etc.

SELECT statements are usually used with

reader = cmd.ExecuteReader();

and you'll need to create a dataReader.


这篇关于如何在数据库中检查所选的下拉列值是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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