0% found this document useful (0 votes)
34 views

Add Record Through Csharp App

This document discusses how to add records to a Microsoft Access database through Csharp code. It defines classes for student information and classes. It includes methods for adding a student record, adding a class record, and listing data from the database in a ListView control. The code opens a connection to an Access database, builds INSERT queries, executes them, and handles any exceptions.

Uploaded by

Mubashar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Add Record Through Csharp App

This document discusses how to add records to a Microsoft Access database through Csharp code. It defines classes for student information and classes. It includes methods for adding a student record, adding a class record, and listing data from the database in a ListView control. The code opens a connection to an Access database, builds INSERT queries, executes them, and handles any exceptions.

Uploaded by

Mubashar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Add record through Csharp in Ms Access database

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace IslamiaySchool.Classes

class classload

public int id { get; set; }

public string classname { get; set; }

namespace IslamiaySchool.Classes

class AddtoDataBase

{
OleDbConnection con = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=.\IslamiyaSchool.accdb");

public void AddStudent(StudentInfo add)

// StudentInfo add =new StudentInfo();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandText = "INSERT INTO StudentInfo([RollNO],[StName], [StFname],


[DOB],[CNIC],[FCelNO], [Address],[ClassName], [AddmDate], [Gender]) VALUES (@rollno,
@stname, @stfname,@dob, @cnic, @fcelno,@address, @classname, @addmdate, @gender)";

con.Open();

cmd.ExecuteNonQuery();

con.Close();

catch (Exception ex)

{using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Text;
using System.Threading.Tasks;

namespace IslamiaySchool.Classes

class StudentInfo

public string RollNo { get; set; }

public string SName { get; set; }

public string SFname { get; set; }

public DateTime DOb { get; set; }

public string Cnic { get; set; }

public string FCelNo { get; set; }

public string Address { get; set; }

public int Fee { get; set; }

public int AddmFee { get; set; }

public string ClassName { get; set; }

public DateTime AddmDate { get; set; }

public string Gender { get; set; }

}
MessageBox.Show(ex.Message);

public void Addclass(AddeClass add)

// StudentInfo add =new StudentInfo();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandText = "INSERT INTO Class([ClassName]) VALUES (@name)";

cmd.Parameters.AddWithValue("@name", add.clsname);

try

con.Open();

cmd.ExecuteNonQuery();

con.Close();

catch (Exception ex)

MessageBox.Show(ex.Message);
}

public void list_DataView(string strSQL, ListView myList, string A)

myList.Items.Clear();

try

OleDbCommand cmd = new OleDbCommand(strSQL, con);

con.Open();

OleDbDataReader dr = cmd.ExecuteReader();

while (dr.Read())

ListViewItem lItem = new ListViewItem();

for (int i = 0; i < dr.FieldCount; i++)

lItem.SubItems[0].Text=A;

lItem.SubItems.Add(dr[i].ToString());
}

myList.Items.Add(lItem);

int rCount = myList.Items.Count;

if (rCount % 2 == 1)

//lItem.BackColor = Color.WhiteSmoke;

lItem.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(208)))),
((int)(((byte)(234)))), ((int)(((byte)(189)))));

else

lItem.BackColor = Color.White;

con.Close();

cmd.Dispose();

dr.Close();

catch (Exception exp)


{

MessageBox.Show(exp.Message);

public void list_DataView(string strSQL, ListView myList )

myList.Items.Clear();

try

OleDbCommand cmd = new OleDbCommand(strSQL, con);

con.Open();

OleDbDataReader dr = cmd.ExecuteReader();

while (dr.Read())

ListViewItem lItem = new ListViewItem(dr[0].ToString());

for (int i = 1; i < dr.FieldCount; i++)

lItem.SubItems.Add(dr[i].ToString());
}

myList.Items.Add(lItem);

int rCount = myList.Items.Count;

if (rCount % 2 == 1)

//lItem.BackColor = Color.WhiteSmoke;

lItem.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(208)))),
((int)(((byte)(234)))), ((int)(((byte)(189)))));

else

lItem.BackColor = Color.White;

con.Close();

cmd.Dispose();

dr.Close();
}

catch (Exception exp)

MessageBox.Show(exp.Message);

con.Close();

You might also like