• Related Post

    Wednesday, July 19, 2017

    Save Table Data in Database on save button Click SQL Server | Code for save table data in SQL Server Database


    Hello Friends

    In this article i am going to tell how to save table data in sql server database on save button click, This article will be very useful for those who want database in their website. for save record etc.

    First Method To save data in SQL Database

    use this following code in C# page

    Note:- use your own database name userid and password, simply copy paste following code and replace your database name userid and password with own

    SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = "Data Source=KVQA-002;Initial Catalog=ERPDB;Integrated Security=true";
    // cnn.ConnectionString = "Server=admin-pc; Database=Login; User Id=sa; password= sa";

            cnn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select*from  Pur_Eval_Supp_Performance”;
            cmd.Connection = cnn;
            SqlDataAdapter da = new SqlDataAdapter();      
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds, "Pur_Eval_Supp_Performance");
               SqlCommandBuilder cb = new SqlCommandBuilder(da);
               DataRow drow = ds.Tables["Pur_Eval_Supp_Performance"].NewRow();
                drow["ESRquarter"] = TextBox1.Text;
                drow["ERSmonth"] = TextBox2.Text;
                drow["ERSratingsch"] = DropDownList1.SelectedItem;
                drow["ERSattitude"] = DropDownList2.SelectedItem;
                ds.Tables["Pur_Eval_Supp_Performance"].Rows.Add(drow);
                da.Update(ds, "Pur_Eval_Supp_Performance");
                MessageBox.Show("Data is Saved Successfully !!!!!!!!!!", "Message");

    //Response.Write(“<script>alert(‘Data is Saved’)</script>”);


    Second Method To save data in SQL Database

    SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = "server=sandeep-pc; database=information; uid=sa; pwd=sa";
                cnn.Open();
                SqlCommand cmd = new SqlCommand("insert into Std_Fee_Details(DTstdid,DTstdname,DTcourse,DTduration,DTsem,DTtotalfee,DTpaid,DTpaidtype,DTchequeno,DTbalance,DTpaydate) values(@DTstdid,@DTstdname,@DTcourse,@DTduration,@DTsem,@DTtotalfee,@DTpaid,@DTpaidtype,@DTchequeno,@DTbalance,@DTpaydate)", cnn);
                cmd.Parameters.AddWithValue("@DTstdid", ddlStd_Id.SelectedItem.Value);
                cmd.Parameters.AddWithValue("@DTstdname",txtStd_Name.Text);
                cmd.Parameters.AddWithValue("@DTcourse", txtCourse.Text);
                cmd.Parameters.AddWithValue("@DTduration", txtDuration.Text);
                cmd.Parameters.AddWithValue("@DTsem", txtSem.Text);
                cmd.Parameters.AddWithValue("@DTtotalfee",txtTotFee.Text);
                cmd.Parameters.AddWithValue("@DTpaid", txtPaid.Text);
                cmd.Parameters.AddWithValue("@DTpaidtype", Convert.ToString(RadioButtonList1.SelectedItem.Value));
                cmd.Parameters.AddWithValue("@DTchequeno",txtCno.Text);
                cmd.Parameters.AddWithValue("@DTbalance", txtBalance.Text);
                cmd.Parameters.AddWithValue("@DTpaydate", txtPaydate.Text);
                cmd.ExecuteNonQuery();

                cnn.Close();

    If you face any difficulty using this code, Please write in comment box i will reply as soon as possible.

    I hope you will find this code helpful.

    For New update or new article dont forget to subscribe.  

    No comments:

    Post a Comment