2
Reply

Error in updating Datalist

Mike Simo

Mike Simo

Jul 23 2014 12:25 PM
701

Hi @JayChase many thanks for your reply, its work but there is something confuse happening as the record for example of post if i change it and click on update all the post for all records in the table will be same, also when i click on update button i need to click it twice then the datalist will be in itemtemplate mode. please check the link below i have record my screen may it will give more info about what is happening with me

https://www.youtube.com/watch?v=hkp6eQTANDc&feature=youtu.be


protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                USRNMElbl.Text = Session["UsrNme"].ToString();

                using (SqlConnection userlistview = new SqlConnection(sc))
                {
                    // Open your connection
                    userlistview.Open();

                    // Build your data adapter
                    SqlDataAdapter userinfoDA = new SqlDataAdapter("SELECT * FROM [UserInfo] WHERE ([UID] = @UID)", sc);

                    // Grab your location (guaranteed to exist from the above if-statement
                    string UsrNme = Convert.ToString(Session["UsrNme"]);

                    // Add your parameters to your data adapter
                    userinfoDA.SelectCommand.Parameters.AddWithValue("@UID", UsrNme);

                    // Define your data set
                    DataSet userinfods = new DataSet();

                    // Fill your data set
                    userinfoDA.Fill(userinfods);

                    // Bind your results
                    userinfo.DataSource = userinfods.Tables[0];
                    userinfo.DataBind();
                }
                //}
                //else
                //{
                //    // Your Session key could not be found, consider displaying an error
                //}
            }
            else
            {
                return;
            }


            if (!Page.IsPostBack)
            {
                DataTable countrycascd = new DataTable();
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
                {
                    SqlDataAdapter adaptar = new SqlDataAdapter("select [countryID],[country] FROM [countr]", con);
                    adaptar.Fill(countrycascd);

                    countrdrdolst.DataSource = countrycascd;
                    countrdrdolst.DataTextField = "country";
                    countrdrdolst.DataValueField = "countryID";
                    countrdrdolst.DataBind();
                }

                countrdrdolst.Items.Insert(0, new ListItem("Välj land", "0"));
            }




           


            if (!Page.IsPostBack)
            {
                DataTable Sectiondt = new DataTable();
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
                {
                    SqlDataAdapter adaptar = new SqlDataAdapter("select [CateID],[Category] FROM [Section]", con);
                    adaptar.Fill(Sectiondt);

                    Catedrdoads.DataSource = Sectiondt;
                    Catedrdoads.DataTextField = "Category";
                    Catedrdoads.DataValueField = "CateID";
                    Catedrdoads.DataBind();
                }

                Catedrdoads.Items.Insert(0, new ListItem("Select Section", "0"));

            }


        }
   
        
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
        }
        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 2;
        }


        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 3;
        }
        
        protected void addadsbtn_Click(object sender, EventArgs e)
        {

            if (SubCatedrdoads.SelectedItem.Text != "Cars")
            {
                //If so then show the panel
                carpanel.Visible = true;
            }
            else
            {
                //If not then hide the panel
                carpanel.Visible = false;
            }   
    
            Guid newGUID = Guid.NewGuid();
            SqlConnection cn = new SqlConnection(sc);
            SqlCommand cmd = new SqlCommand();

            string sqlstatment = "INSERT INTO [ads] ([Section], [Category], [UID], [AdsTit], [AdsDesc], [Country], [State], [City], [AdsPrice], [Img1], [img2], [img3], [img4], [img5], [Wtags], [Address]) VALUES (@Section, @Category, @UID, @AdsTit, @AdsDesc, @Country, @State, @City, @AdsPrice, @Img1, @img2, @img3, @img4, @img5, @Wtags, @Address)";

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

            //Insert the parameters first
            cmd.Parameters.AddWithValue("@Section", Catedrdoads.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@Category", SubCatedrdoads.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@UID", USRNMElbl.Text);
            cmd.Parameters.AddWithValue("@AdsTit", addadstittxtbx.Text);
            cmd.Parameters.AddWithValue("@AdsDesc", adddesctxtbx.Text);
            cmd.Parameters.AddWithValue("@Country", countrdrdolst.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@State", statedrdolst.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@City", citiesdrdolst.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@AdsPrice", adsaddpristxtbx.Text);
            cmd.Parameters.AddWithValue("@Address", addadstxtbox.Text);
            cmd.Parameters.AddWithValue("@Img1", FileUploadImg1.FileName);
            cmd.Parameters.AddWithValue("@Img2", FileUploadImg2.FileName);
            cmd.Parameters.AddWithValue("@Img3", FileUploadImg3.FileName);
            cmd.Parameters.AddWithValue("@Img4", FileUploadImg4.FileName);
            cmd.Parameters.AddWithValue("@Img5", FileUploadImg5.FileName);
            cmd.Parameters.AddWithValue("@Wtags", addadswtagtxtbtn.Text);

            if (FileUploadImg1.HasFile)
            {

                FileUploadImg1.SaveAs(Server.MapPath("~/images/AdsImgs/" + FileUploadImg1.FileName));

            }

            if (FileUploadImg2.HasFile)
            {

                FileUploadImg2.SaveAs(Server.MapPath("~/images/AdsImgs/" + FileUploadImg2.FileName));

            }

            if (FileUploadImg3.HasFile)
            {

                FileUploadImg3.SaveAs(Server.MapPath("~/images/AdsImgs/" + FileUploadImg3.FileName));

            }

            if (FileUploadImg4.HasFile)
            {

                FileUploadImg4.SaveAs(Server.MapPath("~/images/AdsImgs/" + FileUploadImg4.FileName));

            }

            if (FileUploadImg5.HasFile)
            {

                FileUploadImg5.SaveAs(Server.MapPath("~/images/AdsImgs/" + FileUploadImg5.FileName));

            }

            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.SelectCommand = cmd;
            ad.Fill(ds);
            
            Response.Redirect("User panel.aspx");

           
        }


       

        protected void countrdrdolst_SelectedIndexChanged(object sender, EventArgs e)
        {
            int countryID = Convert.ToInt32(countrdrdolst.SelectedValue);
            DataTable StateDT = new DataTable();
            using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
            {
                SqlDataAdapter adaptar = new SqlDataAdapter("Select [StateID],[State] FROM [State] WHERE [countryID] = " + countryID, con2);

                adaptar.Fill(StateDT);

                statedrdolst.DataSource = StateDT;
                statedrdolst.DataTextField = "State";
                statedrdolst.DataValueField = "StateID";
                statedrdolst.DataBind();
            }

            statedrdolst.Items.Insert(0, new ListItem("Välj Län", "0"));
        }

        protected void statedrdolst_SelectedIndexChanged(object sender, EventArgs e)
        {
            int StateID = Convert.ToInt32(statedrdolst.SelectedValue);
            DataTable Citydt = new DataTable();
            using (SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
            {
                SqlDataAdapter adaptar = new SqlDataAdapter("select [CityID],[City] FROM [Cities] WHERE [StateID] = " + StateID, con3);
                adaptar.Fill(Citydt);

                citiesdrdolst.DataSource = Citydt;
                citiesdrdolst.DataTextField = "City";
                citiesdrdolst.DataValueField = "CityID";
                citiesdrdolst.DataBind();
            }

            citiesdrdolst.Items.Insert(0, new ListItem("Välj Stad", "0"));
        }

        protected void citiesdrdolst_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        protected void Catedrdoads_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CateID = Convert.ToInt32(Catedrdoads.SelectedValue);
            DataTable SubCatedt = new DataTable();
            using (SqlConnection con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString))
            {
                SqlDataAdapter adaptar = new SqlDataAdapter("Select [SubcateID],[Subcateg] FROM [SubCate] WHERE [CateID] = " + CateID, con4);

                adaptar.Fill(SubCatedt);

                SubCatedrdoads.DataSource = SubCatedt;
                SubCatedrdoads.DataTextField = "Subcateg";
                SubCatedrdoads.DataValueField = "SubcateID";
                SubCatedrdoads.DataBind();
            }

            SubCatedrdoads.Items.Insert(0, new ListItem("Select Category", "0"));

           
        }


        protected void SubCatedrdoads_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
        {

        }

        protected void showuserads_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void userinfo_EditCommand(object source, DataListCommandEventArgs e)
        {

            userinfo.EditItemIndex = e.Item.ItemIndex;
            DataBind();
        }

        protected void userinfo_CancelCommand(object source, DataListCommandEventArgs e)
        {
            userinfo.EditItemIndex = -1;
            DataBind();
        }

        protected void userinfo_UpdateCommand(object source, DataListCommandEventArgs e)
        {


            if (e.CommandName.Equals("update"))
            {
                //Change the way you are finding control here
                var FNEDTTXTBX = (TextBox)e.Item.FindControl("FNEDTTXTBX");
                var LNEDTTXTBX = (TextBox)e.Item.FindControl("LNEDTTXTBX");
                var PASSEDTTXTBX = (TextBox)e.Item.FindControl("PASSEDTTXTBX");
                var REPASEDTTXTBX = (TextBox)e.Item.FindControl("REPASEDTTXTBX");
                var EMAEDTTXTBX = (TextBox)e.Item.FindControl("EMAEDTTXTBX");
                var PSOTEDTTXTBX = (TextBox)e.Item.FindControl("PSOTEDTTXTBX");
                var regaddrstxtbx = (TextBox)e.Item.FindControl("regaddrstxtbx");
                var EDpersimgFileUpload1 = (FileUpload)e.Item.FindControl("EDpersimgFileUpload1");
                var EDcomplogo = (FileUpload)e.Item.FindControl("EDcomplogo");
                var REWEBSITETXTBX = (TextBox)e.Item.FindControl("REWEBSITETXTBX");
                var UIDTxt = (TextBox)e.Item.FindControl("UIDTxt");
                
                
                SqlConnection upusrinfoDLCON = new SqlConnection(sc);
                SqlCommand upusrinfoDLcmd = new SqlCommand();
                upusrinfoDLcmd.Connection = upusrinfoDLCON;

                upusrinfoDLcmd.CommandText = "Update UserInfo set UID=@6, FN=@1, LN=@2, Password=@3, RePass=@4, Email=@5, Img=@12, Logo=@11,Website=@13 where ([UID] = @6)";

                upusrinfoDLcmd.Parameters.Add("@1", SqlDbType.VarChar, 25).Value = FNEDTTXTBX.Text;
                upusrinfoDLcmd.Parameters.Add("@2", SqlDbType.VarChar, 25).Value = LNEDTTXTBX.Text;
                upusrinfoDLcmd.Parameters.Add("@3", SqlDbType.VarChar, 25).Value = PASSEDTTXTBX.Text;
                upusrinfoDLcmd.Parameters.Add("@4", SqlDbType.VarChar, 25).Value = REPASEDTTXTBX.Text;
                upusrinfoDLcmd.Parameters.Add("@5", SqlDbType.VarChar, 25).Value = EMAEDTTXTBX.Text;
                upusrinfoDLcmd.Parameters.Add("@6", SqlDbType.NVarChar, 20).Value = UIDTxt.Text;
                
                
                upusrinfoDLcmd.Parameters.Add("@11", SqlDbType.NVarChar, -1).Value = EDcomplogo.FileName;
                upusrinfoDLcmd.Parameters.Add("@12", SqlDbType.NVarChar, -1).Value = EDpersimgFileUpload1.FileName;
                upusrinfoDLcmd.Parameters.Add("@13", SqlDbType.NVarChar, 30).Value = REWEBSITETXTBX.Text;


                upusrinfoDLCON.Open();
                upusrinfoDLcmd.ExecuteNonQuery();
                userinfo.EditItemIndex = -1;
                upusrinfoDLCON.Close();
                
            }
        }

        
        protected void userinfo_DeleteCommand(object source, DataListCommandEventArgs e)
        {

        }

        protected void userinfo_ItemCommand(object source, DataListCommandEventArgs e)
        {
            using (SqlConnection userlistview = new SqlConnection(sc))
            {
                // Open your connection
                userlistview.Open();

                // Build your data adapter
                SqlDataAdapter userinfoDA = new SqlDataAdapter("SELECT * FROM [UserInfo] WHERE ([UID] = @UID)", sc);

                // Grab your location (guaranteed to exist from the above if-statement
                string UsrNme = Convert.ToString(Session["UsrNme"]);

                // Add your parameters to your data adapter
                userinfoDA.SelectCommand.Parameters.AddWithValue("@UID", UsrNme);

                // Define your data set
                DataSet userinfods = new DataSet();

                // Fill your data set
                userinfoDA.Fill(userinfods);

                // Bind your results
                userinfo.DataSource = userinfods.Tables[0];
                userinfo.DataBind();
            }
        }
      

    }



Answers (2)