Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();  
                }  
  
            }  
Posted
Updated 7-May-18 23:20pm

1 solution

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.

 
Share this answer
 
v3
Comments
Deekshaa Singh Chauhan 10-May-18 2:36am    
not working
W Balboos, GHB 10-May-18 6:30am    
Try debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900