This is the application I created for my personal use. I need some quality testing of this application. If you see some errors or any lengthy coding (it's already lengthy), and it needs to be shortened please tell me. I would love to learn new things; as I'm a newbie, I have made lots of mistakes here.
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Configuration;
namespace Table_creator
{
public partial class CreateTable : Form
{
private BindingSource bindingSource1 = new BindingSource();
public CreateTable()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string qry = "CREATE TABLE [" + txtCreateTable.Text + "] (StudentName NVARCHAR(50),ContactNo nchar(11),FathersNo nchar(11), MothersNo nchar(11), Email nvarchar(100))";
SqlCommand cmd = new SqlCommand(qry, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Success! Updated");
ListTables();
GetData();
}
catch (System.Exception ex)
{
MessageBox.Show("Failed");
txtCreateTable.Focus();
}
finally
{
con.Close();
}
}
private void ListTables()
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";
SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);
DataTable dtRecords = new DataTable();
dbAdapter.Fill(dtRecords);
comboBox1.DataSource = dtRecords;
comboBox1.DisplayMember = "TABLE_NAME";
con.Close();
}
catch (Exception exx)
{
MessageBox.Show(exx.Message);
}
}
private void btnCreateTable_Load(object sender, EventArgs e)
{
ListTables();
dataGridView1.DataSource = bindingSource1;
GetData();
}
private void GetData()
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);
DataTable dtRecords = new DataTable();
dbAdapter.Fill(dtRecords);
comboBox1.DataSource = dtRecords;
comboBox1.DisplayMember = "TABLE_NAME";
con.Close();
}
catch { }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand("Select * From [" + comboBox1.Text + "]", con);
con.Open();
/* cmd.Connection = con;
// cmd.CommandType = CommandType.Text;
cmd.CommandText =
*/
SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);
DataTable dtRecords = new DataTable();
dbAdapter.Fill(dtRecords);
dataGridView1.DataSource = dtRecords;
//dataGridView1.AutoGenerateColumns = true; //dataGrid
//comboBox1.DataSource = dtRecords;
//comboBox1.DisplayMember = "TABLE_NAME";
con.Close();
}
catch
{ }
}
private void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand("Insert into ["+comboBox1.Text+"](StudentName,ContactNo,FathersNo,MothersNo,Email) values (@StudentName,@ContactNo,@FathersNo,@MothersNo,@Email)", con);
try
{
if (txtName.Text == "")
{
txtName.Focus();
}
else if (txtSNo.Text == "")
{
txtSNo.Focus();
}
else if (txtFNo.Text == "")
{
txtFNo.Focus();
}
else if (txtMNo.Text == "")
{
txtMNo.Focus();
}
else if (txtEID.Text == "")
{
txtEID.Focus();
}
else
{
con.Open();
// assigning values to parameter
cmd.Parameters.AddWithValue("@StudentName", txtName.Text);
cmd.Parameters.AddWithValue("@ContactNo", txtSNo.Text);
cmd.Parameters.AddWithValue("@FathersNo", txtFNo.Text);
cmd.Parameters.AddWithValue("@MothersNo", txtMNo.Text);
cmd.Parameters.AddWithValue("@Email", txtEID.Text);
cmd.ExecuteNonQuery();
// Calling Value Again For refresh
GetData();
// this.
txtName.Clear(); txtSNo.Clear(); txtFNo.Clear(); txtMNo.Clear(); txtEID.Clear();
con.Close();
txtName.Focus();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}