Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;


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();
               
                
            }
        }


Bind to Gridview:

C#
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.
Posted

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.
 
Share this answer
 
v2
Comments
[no name] 7-Mar-14 5:53am    
Thanks alot for showing interest on my question.Now i'm clear.
Hi
Instead of

C#
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 :

C#
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...
 
Share this answer
 
v2

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