Sunday 13 April 2014

Insert data into database in window form


                                                                                                                                               previous
                                                                                                                                                   next
Here i will explain how to insert data into database in window form. Before this i explain that how to open a second form using first form.

Step(1) : Registration form

Here i make a registration form where i take 4 lable and 4 textbox and a button(save).

Step(2) : database

Now make a database into sql server

Step(3) : Code on save button



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           SqlConnection con = new SqlConnection(str);
                String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
                String query = "insert into data (E.id,name,surname,age) values ('"+this.eid_txt.text+"','"+this.nametxt.text+"','"+this.surname_txt.text+"','"+this.age_txt.text+"')";
     SqlCommand cmd = new sqComamnd(query,con);         
  SqlDataReader dbr;
                       
               try
              {
               con.open();
               dbr = cmd.ExecuteReader();
                MessageBox.Show("saved");
              while(dbr.read())
             {
                 
              }
            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);

            }
        }
    }
} 

Step(4) : output


Now click on save button
                                                                                                                                               previous
                                                                                                                                                   next

1 comment:

  1. Error 5 'System.Data.SqlClient.SqlDataReader' does not contain a definition for 'read' and no extension method 'read' accepting a first argument of type 'System.Data.SqlClient.SqlDataReader' could be found (are you missing a using directive or an assembly reference?)

    ReplyDelete

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...