Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
this is code in datalayer

public class Student
{

SqlConnection connection = new SqlConnection("Data Source...............");
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
public DataTable Read()
{
connection.Open();
cmd.CommandText = "SELECT * from Student";
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, connection);

da.Fill(ds);
//cmd.ExecuteReader();
connection.Close();
dt = ds.Tables[0];
return dt;
}
public void Add(string studentName, string FatherName, string ClassName )
{
cmd.CommandText = "INSERT INTO Student (StudentName,FatherName,ClassName) VALUES (@StudentName, @FatherName,@ClassName )";

cmd.Parameters.AddWithValue("@StudentName", studentName);
cmd.Parameters.AddWithValue("@FatherName", FatherName);
//cmd.Parameters.AddWithValue("@DateOfBirth", DateofBirth);
cmd.Parameters.AddWithValue("@ClassName", ClassName);

cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();

}


here i dont wana to use parameter please alternative??????

public void Update(string studentName, string FatherName, string ClassName)
{
cmd.CommandText = "Update Student SET FatherName=@FatherName,ClassName=@ClassName";
cmd.Parameters.AddWithValue("@StudentName", studentName);
cmd.Parameters.AddWithValue("@FatherName", FatherName);
cmd.Parameters.AddWithValue("@ClassName", ClassName);
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;

connection.Open();
cmd.ExecuteNonQuery();

}


public DataTable Clear()
{
SqlCommand cmd = new SqlCommand("DELETE FROM Student", connection);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
return dt;
}


this code in bussinesslayer
here i also dnt wanna use parameter plese tell me alternative



public class BClass
{
public DataTable GetStudentAccount()
{
{
Student stu = new Student();
return stu.Read();

}
}

public DataTable RemoveStudent()
{
{
Student stu = new Student();
return stu.Clear();

}
}

public bool save()
{
Student stu = new Student();

stu.Add("asim", "ali", "5th");
return true;


}

public bool UpdateStudent()
{
Student stu = new Student();

stu.Update("asim", "fazal", "6th");
return true;


}
}

<<big>b>this is behine the presentation layer
kindly tell me alternative way where i dnt wanna use parameter and not entityframework and also use just 3 layer architecture



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






private void button2_Click(object sender, EventArgs e)
{
BClass p = new BClass();
this.dataGridView2.DataSource = p.GetStudentAccount();
}

private void Form1_Load_1(object sender, EventArgs e)
{

}

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button3_Click(object sender, EventArgs e)
{
BClass a = new BClass();
this.dataGridView2.DataSource = a.save();
}

private void button4_Click(object sender, EventArgs e)
{
BClass u = new BClass();
this.dataGridView2.DataSource = u.UpdateStudent();

}

private void button5_Click(object sender, EventArgs e)
{
BClass d = new BClass();
this.dataGridView2.DataSource = d.RemoveStudent();
}




}
Posted

On Create Button Click show pop up and then the user fill the details and on OK click call the function which will take all the values and then update it on the database and then reload the grid.
Do same for all the functionality.
I dont know why you asked this question.
http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htm[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900