参数异常未被用户代码处理 [英] argument exception was unhandled by user code

查看:74
本文介绍了参数异常未被用户代码处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在开发一个网站....按钮点击它应该从texbox中获取数据并将其保存在DB之后它应该显示内容网格视图。在将数据绑定到gridview时将其保存在DB中获取以下错误:靠近

Hi
I'm developing an website....on button click it should fetch the data from texbox and save it in DB after that it should display the content in the grid view.After saving it in DB while Binding the data to gridview Getting the below Error:near

dgWarrenty.DataSource = warrenty;





错误:

dgWarrenty使用了无效的数据源。有效的数据源必须实现IListSource或IEnumerable。



这是我的代码:

保存:



Error:
"An invalid data source is being used for dgWarrenty. A valid data source must implement either IListSource or IEnumerable."

Here is my code:
Save:

protected void BTNSubmit_Click(object sender, EventArgs e)
        {
            if (TXTAssetID.Text == "")
                InsertDepreciationDetails();
                if (TXTAssetID.Text != "")
                {
                    if (TXTSubItem.Text != "" && TXTWarrenty.Text != "" && TXTReferenceNo.Text != "" && TXTWDate.Text != "" && TXTWCompanyName.Text != "")
                    {
                        var newWarrenty = new TB_MasterAssetWarrenty
                        {
                            mAWA_VCCompanyname = TXTWCompanyName.Text,
                            mAWA_VCSubitemname = TXTSubItem.Text,
                            mAWA_DTEExpitydate = DateTime.ParseExact(TXTWDate.Text, @"d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture),
                            mAWA_NUWarrentyterm = mobjGenlib.ConvertDecimal(TXTWarrenty.Text),
                            mAWA_mASS_NUPKId = mobjGenlib.ConvertInt(TXTAssetID.Text),
                            mAWA_VCWarrentyterm_1 = DDLWarrentyPeriod.Text,
                            mAWA_VCReferencenumber = TXTReferenceNo.Text,
                            mAWA_NUIsActive = 1,
                            mAWA_mCMP_NUUniqueId = mobjGenlib.ConvertLong(TXTCompanyID.Text)
                        };
                        //mAWA_mASS_NUPKId = mobjGenlib.ConvertInt(TXTAssetID.Text),

                        DB.TB_MasterAssetWarrenties.InsertOnSubmit(newWarrenty);
                        DB.SubmitChanges();
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, UPWarrenty.GetType(), "ALERT", "alert('The fields should not be left blank');", true);
                        Page.SetFocus(TXTAssetName);
                    }
                


                TB_MasterAssetWarrenty MASACCMULWARR = DB.TB_MasterAssetWarrenties.OrderByDescending(C => C.mAWA_NUPKId).Where(A => A.mAWA_NUIsActive == 1 && A.mAWA_mCMP_NUUniqueId == mobjGenlib.ConvertLong(TXTCompanyID.Text)).FirstOrDefault();
                //Insert In UserLog Report
                mobjGenlib.InsertUserlog("mAWA", mobjGenlib.ConvertString(MASACCMULWARR.mAWA_NUPKId), "I");
                BindWarrentyGrid();
                Disable_Enable_Controls(false);

                //Check User Permission                
                UPWarrenty.Update();
                Disable_Enable_Controls(false);
                IMGSave.Enabled = false;
                IMGSaveCancel.Enabled = false;
                //ImageMode.ImageUrl = "~/ShowImageMode.ashx?Mode=V";
                ImageMode.ImageUrl = "~/ShowImageMode.ashx?Mode=V";
                ShowHideMainGrid();
                ResetWarrentyControl();
               
                
            }
        }





绑定到Gridview:





Bind to Gridview:

public void BindWarrentyGrid()
       {
           UPWarrenty.Update();
           UPWarrentyGrid.Update();
           UPWarrentyGrid.Visible = true;
           string AssetID = TXTAssetID.Text;
           TB_MasterAssetWarrenty warrenty = 
           DB.TB_MasterAssetWarrenties.FirstOrDefault(V => V.mAWA_mASS_NUPKId == mobjGenlib.ConvertLong(TXTAssetID.Text));
           dgWarrenty.DataSource = warrenty;
           dgWarrenty.DataBind();
           dgWarrenty.Visible = true;

       }







任何人都可以说我出错了????



先谢谢。




can any once say were i'm going wrong???

Thanks in Advance.

推荐答案

很明显。正如错误所述,DataGrid希望其数据源实现IListSource或IEnumerable。因此,您无法将不实现这些接口的对象设置为datagrid的数据源,这是有意义的。如果你仍然想要一个记录作为数据源,你可以这样做:



It is obvious. DataGrid expects its data source to implement IListSource or IEnumerable as the error says. Therefore you cannot set an object which doesn't implement those interfaces as data source for datagrid, which makes sense. If you still want a single record as data source, you can do it like that:

dgWarrenty.DataSource = new List<TB_MasterAssetWarrenty>() { warrenty };





正如我经常说的,错误意味着什么。请仔细阅读并尝试了解消息中的内容。



as I say always, errors mean something. please read them carefully and try to understand what is said in the message.


您好b $ b

而不是



Hi
Instead of

public void BindWarrentyGrid()
       {
           UPWarrenty.Update();
           UPWarrentyGrid.Update();
           UPWarrentyGrid.Visible = true;
           string AssetID = TXTAssetID.Text;
          <big> TB_MasterAssetWarrenty warrenty =
           DB.TB_MasterAssetWarrenties.FirstOrDefault(V => V.mAWA_mASS_NUPKId == mobjGenlib.ConvertLong(TXTAssetID.Text));</big>
           dgWarrenty.DataSource = warrenty;
           dgWarrenty.DataBind();
           dgWarrenty.Visible = true;

       }





我更改如下所示:





I Changed like shown below :

public void BindWarrentyGrid()
        {
            UPWarrenty.Update();
            UPWarrentyGrid.Update();
            UPWarrentyGrid.Visible = true;
            string AssetID = TXTAssetID.Text;
            
          <big>  var warrenty = from C in DB.TB_MasterAssetWarrenties
                          where C.mAWA_NUIsActive == 1 && C.mAWA_mASS_NUPKId == mobjGenlib.ConvertInt(AssetID)
                           select new { mAWA_NUPKId = C.mAWA_NUPKId, mAWA_mASS_NUPKId = C.mAWA_mASS_NUPKId, mAWA_VCCompanyname = C.mAWA_VCCompanyname, mAWA_VCSubitemname = C.mAWA_VCSubitemname, mAWA_VCReferencenumber = C.mAWA_VCReferencenumber };</big>
            dgWarrenty.DataSource = warrenty;
            dgWarrenty.DataBind();
            dgWarrenty.Visible = true;

        }





现在工作正常......



Now it works fine...


这篇关于参数异常未被用户代码处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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