Wednesday, November 10, 2021

Insert statement or query in asp.net

<configuration>

<connectionStrings>

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

</connectionStrings>

</configuration>

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

protected void btnAdd_Click(object sender, EventArgs e)

    {

        String conStr = ConfigurationManager.ConnectionStrings["connectionStringTest"].ConnectionString;


        using (SqlConnection con = new SqlConnection(conStr))

        {

            lblUniqueID.Text = Guid.NewGuid().ToString();

            using (SqlCommand cmd = new SqlCommand("INSERT INTO TEST1 VALUES('"+txtName.Text+"'," 

                + "'" +lblUniqueID.Text.ToString()+"')", con))

            {

                con.Open();


                int a = cmd.ExecuteNonQuery();

                con.Close();

                }

        }

    }

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

<div>

            <asp:Label ID="lblName" runat="server" Text="Name" />

            <asp:TextBox ID="txtName" runat="server" />


            <asp:Label runat="server" ID="lblUniqueID" />


            <asp:Button runat="server" ID="btnAdd" Text="Add Test" OnClick="btnAdd_Click" />

        </div>

No comments:

Post a Comment