Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everybody I have a question this is my populating data codes

C#
public void Populateicerik()
        {
            string bag = WebConfigurationManager.ConnectionStrings["AdminPanel.Properties.Settings.ConnStr"].ConnectionString;
            SqlConnection con = new SqlConnection(bag);

            string komut = "select icerikID,KBaslik,icerikDurum,icerikErisim,icerikMetin from icerik inner join kategoriler on KID = KategoriID";
            SqlCommand cmd = new SqlCommand(komut, con);

            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            GridView3.DataSource = dt;
            GridView3.DataBind();
            con.Open();
            con.Close();

            GridView3.HeaderRow.Cells[1].Text = "iID";
            GridView3.HeaderRow.Cells[2].Text = "Kategori";
            GridView3.HeaderRow.Cells[3].Text = "Durum";
            GridView3.HeaderRow.Cells[4].Text = "Erişim";
            GridView3.HeaderRow.Cells[5].Text = "İçerik";
        }

and this delete codes
protected void LinkButton2_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView3.Rows)
            {
                var chk = row.FindControl("Chk") as CheckBox;

                if (chk.Checked)
                {
                    string bag = WebConfigurationManager.ConnectionStrings["AdminPanel.Properties.Settings.ConnStr"].ConnectionString;
                    SqlConnection con = new SqlConnection(bag);

                    SqlCommand cmd = new SqlCommand("icerik_Delete", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    
                    cmd.Parameters.AddWithValue("icerikID", GridView3.SelectedRow.Cells[1].Text);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
this is not working gridview3.selectedrow turns as null what shoul I do to make it correct please help thank you .
Posted
Updated 26-Jun-13 0:38am
v2

You can try below code

protected void LinkButton2_Click(object sender, EventArgs e)
       {
           foreach (GridViewRow row in GridView3.Rows)
           {
               var chk = row.FindControl("Chk") as CheckBox;

               if (Request[chk.UniqueID] != null)
               {
                   string bag = WebConfigurationManager.ConnectionStrings["AdminPanel.Properties.Settings.ConnStr"].ConnectionString;
                   SqlConnection con = new SqlConnection(bag);

                   SqlCommand cmd = new SqlCommand("icerik_Delete", con);
                   cmd.CommandType = CommandType.StoredProcedure;

                   cmd.Parameters.AddWithValue("icerikID", GridView3.SelectedRow.Cells[1].Text);
                   con.Open();
                   cmd.ExecuteNonQuery();
                   con.Close();
               }
           }
       }
 
Share this answer
 
thank you very much but I fixed it by using this

C#
cmd.Parameters.AddWithValue("@icerikID", SqlDbType.Int);
                    cmd.Parameters["@icerikID"].Value = GridView3.Rows[row.RowIndex].Cells[1].Text;
 
Share this answer
 

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