Wednesday, November 24, 2021

Select and Delete SQL query grid with bind data Asp.net, C#

CS file Code

 private String conStr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            this.bindGrid();

        }

    }


    private void bindGrid()

    {

        using (SqlConnection con = new SqlConnection(conStr))

        {

            //con.Open();

            using (SqlCommand cmd = new SqlCommand("Select * from aggregate.BUILD_UNTITLED_FORM_1636349418_CORE", con))

            {

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))

                {

                    DataTable dt = new DataTable();

                    da.Fill(dt);

                    //con.Close();

                    GridView1.DataSource = dt;

                    GridView1.DataBind();


                }

            }

        }

    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

               using (SqlConnection con = new SqlConnection(conStr))

        {

            using (SqlCommand cmd = new SqlCommand("DELETE FROM aggregate.BUILD_UNTITLED_FORM_1636349418_CORE" +

                " WHERE _uri='" + e.Values["_URI"] + "'", con))

            {

                con.Open();

                cmd.ExecuteNonQuery();

                con.Close();

                this.bindGrid();

            }

        }

    }

----------------------------**************---------------------------

aspx.cs page (UI part)

 <asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" 

                AutoGenerateEditButton="True" AutoGenerateSelectButton="True" 

                AllowSorting="True" OnRowDeleting="GridView1_RowDeleting" >

            </asp:GridView>

--------------------------***************------------------------------web.config file code below

<configuration>

<connectionStrings>

    <add name="connectionString" connectionString="initial catalog=aggregate;user=sa1;password=sa;data source=." />

   </connectionStrings>

</configuration>

-------------------------**************--------------------------table structure (screenshot below)




No comments:

Post a Comment