Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have two ListView the main one which will show you your CV Called CVLISTVIEW and inside this Listview there is Edit button called: EditCVBtn the code of this button is:

C#
protected void EditCVBtn_Command(object sender, CommandEventArgs e)
       {
           ShowCVPanel.Visible = false;
           editCVPanel.Visible = true;

           if (Session["UsrNme"] != null)
           {

               using (SqlConnection RedMsgSQLCon = new SqlConnection(sc))
               {
                   RedMsgSQLCon.Open();

                   SqlDataAdapter RedMsgDAADP = new SqlDataAdapter(@"SELECT * From CVs where UID=@UID And CVNum=@CVNum", sc);

                   var use = Session["UsrNme"];

                   Control myControl = FindControl("CVNumlbl");

                   RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@UID", use);
                   RedMsgDAADP.SelectCommand.Parameters.AddWithValue("@CVNum", e.CommandArgument);

                   DataSet RedMsgCVs = new DataSet();

                   RedMsgDAADP.Fill(RedMsgCVs);

                   EditCVlistVw.DataSource = RedMsgCVs.Tables[0];
                   EditCVlistVw.DataBind();
               }
           }
           else
           {
               // Your Session key could not be found, consider displaying an error
           }

       }



Now as it mention into EditCVBtn code is to show the another ListView called EditCVlistVw someone will ask why you use two Listviews one to show CV and other to edit it, My answer is very simple as if I use one ListView with ItemTemplate and EditItemTemplate I will face many of problem with Cascading DropDownList as well as RequiredFieldValidator as well FileUpload. So now what is happen in the 2nd Listview (the one where you edit your CV) is when I click on UpdtCVBtn which is located into EditCVlistVw and behind code of this button is:

C#
protected void UpdtCVBtn_Command(object sender, CommandEventArgs e)
       {

           SqlConnection EdCVCon = new SqlConnection(sc);
           SqlCommand cmd = new SqlCommand();

           if (Session["UsrNme"] != null)
           {


               string sqlstatment = @"Update CVs SET Birthday=@Birth, Gender=@Gend, PerInfo=@Pinfo, WorkEx=@WEXP, Educ=@Edu, SchAndCert=@SHOLcERT, Langu=@Lang, Hobbies=@Hobis, citiz=@Citzn, Linkden=@Linkden, PersWebsite=@PWeb, FN=@fn, LN=@ln, EMail=@mail, TeleNo=@tele, PersImg=@PIMG Where  CVNum=@CVN)";

               cmd.Connection = EdCVCon;
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = sqlstatment;

               cmd.Parameters.AddWithValue("@Birth", ((TextBox)EditCVlistVw.FindControl("UpdBirthdTxtBox")).Text);
               cmd.Parameters.AddWithValue("@Gend", ((DropDownList)EditCVlistVw.FindControl("UpdGendrDDL")).SelectedValue);
               cmd.Parameters.AddWithValue("@Pinfo", ((TextBox)EditCVlistVw.FindControl("UpdPerInfoTXT")).Text);
               cmd.Parameters.AddWithValue("@WEXP", ((TextBox)EditCVlistVw.FindControl("UpdWorkExtxt")).Text);
               cmd.Parameters.AddWithValue("@Edu", ((TextBox)EditCVlistVw.FindControl("UpdEducTxt")).Text);
               cmd.Parameters.AddWithValue("@SHOLcERT", ((TextBox)EditCVlistVw.FindControl("UpdSchAndCertTXT")).Text);
               cmd.Parameters.AddWithValue("@Lang", ((TextBox)EditCVlistVw.FindControl("UpdLanguTxt")).Text);
               cmd.Parameters.AddWithValue("@Hobis", ((TextBox)EditCVlistVw.FindControl("UpdHobbiesTxt")).Text);
               cmd.Parameters.AddWithValue("@Citzn", ((DropDownList)EditCVlistVw.FindControl("UpdJOBworlddrdolist")).SelectedValue);
               cmd.Parameters.AddWithValue("@Linkden", ((TextBox)EditCVlistVw.FindControl("LinkdenTextBox")).Text);
               cmd.Parameters.AddWithValue("@PWeb", ((TextBox)EditCVlistVw.FindControl("PWebtxtB")).Text);
               cmd.Parameters.AddWithValue("@fn", ((TextBox)EditCVlistVw.FindControl("FNEditTxtBx")).Text);
               cmd.Parameters.AddWithValue("@ln", ((TextBox)EditCVlistVw.FindControl("LNEditTxtBx")).Text);
               cmd.Parameters.AddWithValue("@mail", ((TextBox)EditCVlistVw.FindControl("EmailEditTxt")).Text);
               cmd.Parameters.AddWithValue("@tele", ((TextBox)EditCVlistVw.FindControl("TeleEditTxtB")).Text);
               cmd.Parameters.AddWithValue("@PIMG", ((FileUpload)EditCVlistVw.FindControl("FileUpload1")).FileName);


               EdCVCon.Open();
               int result = cmd.ExecuteNonQuery();
               if (result > 0)
               {
                   // Updated successfully;
               }
               editCVPanel.Visible = false;
               ShowCVPanel.Visible = true;
           }

       }


When I click on UpdtCVBtn I am facing this error message with all controls inside this ListView so how I can fix that??!!

Object reference not set to an instance of an object.

this error message show next to each line of controls

C#
cmd.Parameters.AddWithValue("@Birth", ((TextBox)EditCVlistVw.FindControl("UpdBirthdTxtBox")).Text);
Posted
Comments
[no name] 20-Jul-15 10:55am    
The message means exactly what it says. Your controls are not being found on your web page.
Member 10690878 20-Jul-15 20:02pm    
So but the controls are already into the listview so how i can fix it??!!
F-ES Sitecore 20-Jul-15 16:09pm    
Your listview is bound to a table that has anything from 0 to 1,000,000 rows, so when you do this

cmd.Parameters.AddWithValue("@Birth", ((TextBox)EditCVlistVw.FindControl("UpdBirthdTxtBox")).Text);

there are between 0 and 1,000,000 "UpdBirthdTxtBox" controls, so which one are you expecting to be returned? How does .net know which exact control you want out of all the possible ones? Look at the Items collection, find the right one and use FindControl on that.
Member 10690878 20-Jul-15 20:41pm    
there is only this Listview and there is only those controls inside it, i agree with you the listview will repeat many times for same control as much as you have into table into database. But i am showing only one record for the same userid which is under session value. so could you explain more of how i can fix it.
F-ES Sitecore 21-Jul-15 4:19am    
I've already said...these controls contain a collection that represents all the rows\items in it and you need to do FindControl on the right item, so if there is only one row then it will be on the first item in the collection. I'm not that familiar with ListViews but I think the collection is called Items, if that's not it then look at the other collections in the debugger, or just google "asp.net find control listview" and I'm sure you'll find the relevant code.

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