display more than one database query in .net MVC - c#

I am creating my first project using asp.net MVC - I have successfully connected to the database and displayed the information on the index page. My question is how do I get more than one query result on the one index page
for e.g
SELECT student ID,first name,surname FROM STUDENT Notes WHERE student ID = 7
Do I need to create new controllers/models for each query or need to add to the current and if I add to the current how would I do it? Below is the code I currently have in my controller.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
//int sNumber = 1;
List<CustomerModel> customers = new List<CustomerModel>();
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string query = "SELECT title, `first name`, surname FROM `STUDENT REGISTER`";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new CustomerModel
{
// CustomerId = Convert.ToInt32(sdr["Student Number"]),
Title = sdr["title"].ToString(),
Name = sdr["first name"].ToString(),
Surname = sdr["surname"].ToString()
});
}
}
con.Close();
}
}
return View(customers);
}

Create a view model with two result set for e.g. Student and Marks
public class Result
{
public Student Student { get; set; }
public Marks Marks { get; set; }
}
Load/Construct this Result view model in controller /service with appropriate data and pass this view model to view.
I hope this helps!

You have to create a new class with all the properties you want to display in your View.
Example:
public class StudentModel {
public int Id { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public strign Surname { get; set; }
}
public class MarkModel {
public int Id { get; set; }
public int StudentId { get; set; }
public int SubjectId { get; set; }
public int Mark { get; set; }
}
public class ResultModel
{
public StudentModel Student { get; set; }
public List<MarkModel> Marks { get; set; }
}
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
//int sNumber = 1;
var model= new ResultModel{
Student = new StudentModel(),
Marks = new List<MarkModel>();
}
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string queryStudent = "SELECT id, title, `first name`, surname FROM `STUDENT` WHERE Id=1";
using (MySqlCommand cmd = new MySqlCommand(queryStudent))
{
cmd.Connection = con;
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
model.student.Id = Convert.ToInt32(sdr["id"]),
model.student.Title = sdr["title"].ToString(),
model.student.Name = sdr["first name"].ToString(),
model.student.Surname = sdr["surname"].ToString()
}
}
}
string queryMarks = "SELECT Id, `StudentId`, StudentId,Mark FROM `MARK` WHERE StudentId=1";
using (MySqlCommand cmd = new MySqlCommand(queryMarks))
{
cmd.Connection = con;
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
model.Marks.Add(new MarkModel
{
Id = Convert.ToInt32(sdr["Id"]),
StudentId = Convert.ToInt32(sdr["StudentId"]),
StudentId = Convert.ToInt32(sdr["StudentId"]),
Mark = Convert.ToInt32(sdr["Mark"]),
});
}
}
}
}
return View(model);
}

You should create a new ViewModel class with all the properties you want to display in your View. Then you model your View after it.
From the properties you provided so far, the class should look like this:
public class StudentViewModel {
public int Id { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public strign Surname { get; set; }
}
Then you do the value x property assignment
string query = "SELECT title, `first name`, surname FROM `STUDENT REGISTER`";
List<StudentViewModel> model = new List<StudentViewModel>();
using (MySqlCommand cmd = new MySqlCommand(query)) {
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
model.Add(new StudentViewModel
{
Id = Convert.ToInt32(sdr["StudentNumber"]),
Title = Convert.ToString(sdr["title"]),
Name = Convert.ToString(sdr["first name"]),
Surname = Convert.ToString(sdr["surname"])
});
}
}
con.Close();
}
return View(model);

Related

How to show all data from c#

I'm kind a new on c#. I have a problem with to store the className to list since i need to display all the class that teacher taught. On result, it turns out just the last class teacher taught. I did use join table between teacher and classes.
Model
public class Teacher
{
public int teacherId { get; set; }
public string teacherfName { get; set; }
public string teacherlName { get; set; }
public string className { get; set; }
public int classId { get; set; }
}
Controller
public Teacher FindTeacher(int id)
{
Teacher newTeacher = new Teacher();
MySqlConnection Conn = school.AccessDatabase();
Conn.Open();
MySqlCommand cmd = Conn.CreateCommand();
//SQL QUERY
cmd.CommandText = "Select * from teachers left join classes on teachers.teacherid=classes.teacherid where teachers.teacherid = " + id;
//Gather Result Set of Query into a variable
MySqlDataReader ResultSet = cmd.ExecuteReader();
while (ResultSet.Read())
{
int teacherId = (int)ResultSet["teacherId"];
string teacherfName=ResultSet["teacherfname"].ToString();
string teacherlName=ResultSet["teacherlname"].ToString();
newTeacher.teacherId = teacherId;
newTeacher.teacherFName = teacherFName;
newTeacher.teacherLName = teacherLName;
newTeacher.className = className;
newTeacher.classId = (int)ResultSet["classid"];
}
return newTeacher;
}
Your only returning one teacher if you want all the teachers your code should be:
public IEnumerable<Teacher> FindTeacher(int id)
{
//Lise here
List<Teacher> teachers = new List<Teacher>();
//note the using
using MySqlConnection Conn = school.AccessDatabase();
Conn.Open();
//note the using
using MySqlCommand cmd = Conn.CreateCommand();
//SQL QUERY
cmd.CommandText = "Select * from teachers left join classes on teachers.teacherid=classes.teacherid where teachers.teacherid = " + id;
//Gather Result Set of Query into a variable
MySqlDataReader ResultSet = cmd.ExecuteReader();
while (ResultSet.Read())
{
//new teacher in the loop
Teacher newTeacher = new Teacher();
int teacherId = (int)ResultSet["teacherId"];
string teacherfName=ResultSet["teacherfname"].ToString();
string teacherlName=ResultSet["teacherlname"].ToString();
newTeacher.teacherId = teacherId;
newTeacher.teacherFName = teacherFName;
newTeacher.teacherLName = teacherLName;
newTeacher.className = className;
newTeacher.classId = (int)ResultSet["classid"];
//add to the collection
teachers.Add(newTeacher);
}
//return the collection
return teachers;
}
If also added using statements. These are important to prevent memory leaks
Modify Teacher Class to be able to carry List of TeacherClass that correspond to one teacher:
Define New Class TeacherClass to Carry a TeacherClass Data
public class TeacherClass
{
public string Name { get; set; }
public int Id { get; set; }
}
Modify Teacher Class To have a List Of TeacherClass
public class Teacher
{
public int teacherId { get; set; }
public string teacherfName { get; set; }
public string teacherlName { get; set; }
public List<TeacherClass> classes { get; set; } = new List<TeacherClass>();
}
Then get your function to set this TeacherClass List in a loop:
public Teacher FindTeacher(int id)
{
Teacher newTeacher = new Teacher();
//note the using
using MySqlConnection Conn = school.AccessDatabase();
Conn.Open();
//note the using
using MySqlCommand cmd = Conn.CreateCommand();
//SQL QUERY
cmd.CommandText = "Select * from teachers left join classes on teachers.teacherid=classes.teacherid where teachers.teacherid = " + id;
//Gather Result Set of Query into a variable
MySqlDataReader ResultSet = cmd.ExecuteReader();
// Check if any rows retrieved
if (reader.HasRows)
{
// Iterate Over Rows
while (ResultSet.Read())
{
// Set Teacher Data Just Once
if(newTeacher.teacherId == 0){
newTeacher.teacherId = (int)ResultSet["teacherId"];;
newTeacher.teacherFName = ResultSet["teacherfname"].ToString();
newTeacher.teacherLName = ResultSet["teacherlname"].ToString();
}
// Add new TeacherClass data for this teacher
newTeacher.classes.Add(
new TeacherClass(){
Name = className, // className Check this variable as it is not declared
Id = (int)ResultSet["classid"]
});
}
}
return newTeacher;
}

Using web service to retrieve more than one database column

I am trying to use a web service to retrieve data for more than one column. The code below works fine :
public string GetCustomerNameWithIDNumber(string id_number)
{
string fullname ="";
string id_type = "";
string id_number = "";
string dob = "";
using (SqlConnection cn = new SqlConnection(constring))
{
cn.Open();
string q = "select fullname, id_type, id_number, date_ofbirth from account_info where id_number = #id_number";
using (SqlCommand cmd = new SqlCommand(q, cn))
{
cmd.Parameters.AddWithValue("#id_number", id_number);
using (SqlDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
fullname = rd["fullname"].ToString();
id_type = rd["id_type"].ToString();
id_number = rd["id_number"].ToString();
dob = rd["date_ofbirth"].ToString();
bvn2 = rd["bvn"].ToString();
}
}
return fullname;
}
}
}
Which just gets the Full name alone, now suppose i want to get for more than just one database column, how do I go about it?
You will want to return a class. Here is a simple class I wrote to hold this data:
class Customer {
public string FullName { get; set; }
public string IdType { get; set; }
public string IdNumber { get; set; }
public string DateOfBirth { get; set; }
public string Bvn { get; set; }
}
Then this method returns that customer object:
public Customer GetCustomerNameWithIDNumber(string id_number) {
using (SqlConnection cn = new SqlConnection(constring)) {
cn.Open();
string q = "select fullname,id_type,id_number,date_ofbirth from account_info where id_number =#id_number";
using (SqlCommand cmd = new SqlCommand(q, cn)) {
cmd.Parameters.AddWithValue("#id_number", id_number);
using (SqlDataReader rd = cmd.ExecuteReader()) {
while (rd.Read()) {
return new Customer {
FullName = rd["fullname"].ToString(),
IdType= rd["id_type"].ToString(),
IdNumber = rd["id_number"].ToString(),
DateOfBirth = rd["date_ofbirth"].ToString(),
Bvn = rd["bvn"].ToString()
};
}
}
}
}
}
This code will return the first customer in the database matching the id_number

MVC DataReader Timeout Error

I have this problem with error timeout, always when it suppose to get data from database it bounce error. I am trying to retrieve video from database with this code:
Repository.cs
public List<videoTble> Video()
{
var model = new List<videoTble>();
string ConStr = "Data Source="";Connect Timeout=60";
using (SqlConnection con = new SqlConnection(ConStr))
{
string str = "SELECT * FROM videoTbles";
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandTimeout = 60;
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
var v = new videoTble();
v.Name = rd["Name"].ToString();
v.Data = (byte[])rd["Data"];
v.ContentType = rd["ContentType"].ToString();
v.ArtistName = rd["ArtistName"].ToString();
v.Expirydate = (DateTime)rd["Expirydate"];
model.Add(v);
}
con.Close();
}
return model;
}
Controller.cs
public ActionResult Download()
{
Repository res = new Repository();
ViewBag.Video = res.Video();
return View();
}
Model.cs
public partial class videoTble
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string ContentType { get; set; }
public byte[] Data { get; set; }
public string ArtistName { get; set; }
public DateTime Expirydate { get; set; }
}
The timeout Error I got, when i run application
It looks like you are storing your video as raw data in your database. I highly advise against this. Store the video files on your file system, and store the paths to the video files in your database.

How to correct DoNotExposeGenericLists error C#

I have the following method in DAL which accepts the model OrderList and returns a List<OrderList>
public List<OrderList> GetOrderList(OrderList orderList)
{
...
return new List<OrderList>();
}
When I analyze using FXCop, it's saying DoNotExposeGenericLists Error
FXCop is saying this as resolution
"Change 'List<OrderList>' in 'OrderRelatedDataAccess.GetOrderList(OrderList)' to use
Collection<T>, ReadOnlyCollection<T> or KeyedCollection<K,V>"
How can I correct this?
My OrderList model is shown below
public int serialNumber { get; set; }
public int orderID { get; set; }
public int orderListID { get; set; }
public int productID { get; set; }
public int quantity { get; set; }
public long price { get; set; }
public string productName { get; set; }
Thanks.
Edit:
Code inside the method
List<OrderList> listOfOrderList = new List<OrderList>();
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("sGetOrderListByOrderID", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("#orderID", orderList.orderID);
connection.Open();
reader = command.ExecuteReader();
int sno = 0;
while (reader.Read())
{
long price = long.Parse(reader[4].ToString());
listOfOrderList.Add(new OrderList()
{
serialNumber = ++sno,
orderID = (int)reader[0],
productID = (int)reader[2],
quantity = (int)reader[3],
price = price,
productName = reader[5].ToString(),
});
}
connection.Close();
}
return listOfOrderList;
In your case its best to change the method signature to use an IList instead of List
public IList<OrderList> GetOrderList(OrderList orderList)
{
//get your data and build your list here
return new List<OrderList>();
}
See the differences between ICollection and IList in this answer
The answer is right there in the FxCop message. Just change the return type to be ICollection<OrderList>, like so:
public ICollection<OrderList> GetOrderList(OrderList orderList)
{
...
return new List<OrderList>();
}

The model item passed into the dictionary is of type error

I am using multiple model a single razor page but i getting error when i retrieving data according by specified id.Error (The model item passed into the dictionary is of type 'Proj.Model.PersonInfo', but this dictionary requires a model item of type 'Proj.Model.Person'.)
public class PersonInfo
{
public int PersonId{get;set;}
public string Firstname{get;set;}
public string Lastname{get;set;}
}
public class ContactInfo
{
public int PersonId{get;set;}
public string Address{get;set;}
public string Telephone{get;set;}
}
public class Person
{
public PersonInfo PersonInfo{get;set;}
public ContactInfo ContactInfo{get;set;}
}
public ActionResult PersonInfo(int id)
{
return View(db.GetPersonInfo().FirstOrDefault(m => m.PersonId == id));
}
public List<PersonInfo> GetPersonInfo()
{
List<PersonInfo> info = null;
using (OracleConnection conn = new OracleConnection(_conn))
{
conn.Open();
string query = "SELECT * FROM PERSON_INFO";
using (OracleCommand cmd = new OracleCommand(query, conn))
{
cmd.CommandType = System.Data.CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
info = new List<PersonInfo>();
while (reader.Read())
{
info.Add(new PersonInfo
{
PersonId = Convert.ToInt32(reader["P_ID"]),
Firstname = reader["FIRST_NAME"],
Lastname = reader["LAST_NAME"].ToString()
});
}
}
}
}
return info;
}
Try changing this from
public ActionResult PersonInfo(int id)
{
return View(db.GetPersonInfo().FirstOrDefault(m => m.PersonId == id));
}
to
public ActionResult PersonInfo(int id)
{
var model = new Person { PersonInfo = db.GetPersonInfo().FirstOrDefault(m => m.PersonId == id)}
return View(model);
}

Categories