Here, I will explain how to insert data into a database in Windows Forms. Before this I explained how to open a second form using a first form.
Step1: Registration form
Here I make a registration form where I use 4 lables and 4 TextBoxes and a button ( for save).
Step 2: Database
Now make a database in SQL Server.
Step 3: Code for the 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)
{
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 + "')";
SqlDataReader dbr;
try
{
con.open();
dbr = cmd.ExecuteReader();
MessageBox.Show("saved");
while (dbr.read())
{
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
}
}
Step 4: Output