samedi 26 mai 2018

how to solve error "The given key was not present in the dictionary." when using data set with MySqlDataAdapter?

I try to make connection with MySQL db using this class.

public class DataManager
{
    // Connection String
    public static string constr = ConfigurationManager.ConnectionStrings["cnn1"].ConnectionString; 

    public static DataSet GetDataSet(string stored_name, string table_name, params MySqlParameter[] prmarr)
    {
        MySqlConnection con = new MySqlConnection(constr);
        MySqlCommand cmd = new MySqlCommand(stored_name,con);
        cmd.CommandType = CommandType.StoredProcedure;

        foreach (MySqlParameter prm in prmarr)
        {
            cmd.Parameters.Add(prm);
        }
        DataSet ds = new DataSet();
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);

        da.Fill(ds,table_name);
        return ds;
    } 

then I called it in load event of page that is inhered from master page

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {  
         // product_select is stored procedure/ routine that I made it in db
                using (DataSet ds = DataManager.GetDataSet("product_select","x")) // error is here
                {
                    DataList1.DataSource = ds.Tables["x"];   
                    DataList1.DataBind();
                }
            }
        }

when I run,I get this error.

An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
{"The given key was not present in the dictionary."}




Aucun commentaire:

Enregistrer un commentaire