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
Related
I am new to Web API and I have a questions: how to import Web API data into a SQL Server database.
My Web API is created with data reader and I have searched in the internet, it seems like it could be import data with data reader. I want to test importing data with Web API, so I can know is my Web API working or not.
Code:
[HttpGet]
public IHttpActionResult GetAll()
{
List<webapi> web = new List<webapi>();
using (SqlCommand cmd = new SqlCommand("SELECT U.UserID, U.Name, U.Mobile, U.Age, U.Birthday, A.Address, A.Country From tbluser U inner join tblAddress A where A.Country=U.Country", connection))
{
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
web.Add(new webapi()
{
UserId = Convert.ToString(sdr.GetValue(0)),
Name = Convert.ToString(sdr.GetValue(01)),
Mobile = Convert.ToString(sdr.GetValue(2)),
Age = (sdr.GetValue(3) != DBNull.Value) ? Convert.ToInt32(sdr.GetValue(3)) : 0,
Birthday = (sdr.GetValue(4) != DBNull.Value) ? Convert.ToDateTime(sdr.GetValue(4)) : (DateTime?)null
Address = Convert.ToString(sdr.GetValue(5)),
Country = Convert.ToString(sdr.GetValue(6))
});
}
return Ok(web);
}
}
[HttpGet]
public IHttpActionResult GetCountry(string country)
{
List<webapi> web = new List<webapi>();
using (SqlCommand cmd = new SqlCommand("SELECT U.UserID, U.Name, U.Mobile, U.Age, U.Birthday, A.Address, A.Country From tbluser U inner join tblAddress A where charindex(#country, A.Country)=1 order by U.UserId", connection))
{
cmd.Parameters.Add("#country", SqlDbType.VarChar);
cmd.Parameters["#country"].Value = country.ToString();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
web.Add(new webapi()
{
UserId = Convert.ToString(sdr.GetValue(0)),
Name = Convert.ToString(sdr.GetValue(01)),
Mobile = Convert.ToString(sdr.GetValue(2)),
Age = (sdr.GetValue(3) != DBNull.Value) ? Convert.ToInt32(sdr.GetValue(3)) : 0,
Birthday = (sdr.GetValue(4) != DBNull.Value) ? Convert.ToDateTime(sdr.GetValue(4)) : (DateTime?)null
Address = Convert.ToString(sdr.GetValue(5)),
Country = Convert.ToString(sdr.GetValue(6))
});
}
}
return Ok(web);
}
}
}
Class:
public class webapi
{
public string UserId { get; set; }
public string Name { get; set; }
public string Mobile { get; set; }
public int Age { get; set; }
public DateTime? Birthday { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}
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;
}
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);
I am getting error Not all paths return a value. Its a syntax error how to correct it. here is my code. I am writing this code in class.
public class Employees
{
public String emp_id { get; set; }
public String emp_name { get; set; }
public String u_name { get; set; }
public String pass { get; set; }
public String mail { get; set; }
public String address { get; set; }
public String city { get; set; }
public String dob { get; set; }
public String cnic { get; set; }
public String designation { get; set; }
public String ph_no { get; set; }
}
public class #object
{
public static List<Employees> GetAllEmployees()
{
List<Employee> listemp = new List<Employee>();
string cs = ConfigurationManager.ConnectionStrings[#"Data Source = localhost; Initial Catalog=fms; User=root; Pooling=false; Integrated Security = false"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(cs))
{
MySqlCommand cmd = new MySqlCommand("Select * from emp", con);
con.Open();
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employees em = new Employees();
em.emp_id = dr[0].ToString();
em.emp_name = dr[1].ToString();
em.u_name = dr[2].ToString();
em.pass = dr[3].ToString();
em.mail = dr[4].ToString();
em.address = dr[5].ToString();
em.city = dr[6].ToString();
em.dob = dr[7].ToString();
em.cnic = dr[8].ToString();
em.designation = dr[9].ToString();
em.ph_no = dr[10].ToString();
}
listemp.Add(em);
}
}
}
I attached a pic where I am getting this error.
You should return the List listemp . Also consider moving listemp.Add(em) inside while loop, otherwise you wont get a list
public static List<Employees> GetAllEmployees()
{
List<Employee> listemp = new List<Employee>();
string cs = ConfigurationManager.ConnectionStrings[#"Data Source = localhost; Initial Catalog=fms; User=root; Pooling=false; Integrated Security = false"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(cs))
{
MySqlCommand cmd = new MySqlCommand("Select * from emp", con);
con.Open();
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employees em = new Employees();
em.emp_id = dr[0].ToString();
em.emp_name = dr[1].ToString();
em.u_name = dr[2].ToString();
em.pass = dr[3].ToString();
em.mail = dr[4].ToString();
em.address = dr[5].ToString();
em.city = dr[6].ToString();
em.dob = dr[7].ToString();
em.cnic = dr[8].ToString();
em.designation = dr[9].ToString();
em.ph_no = dr[10].ToString();
listemp.Add(em);
}
}
return listemp;
}
You have two problems: GetAllEmployees() should return List<Employees> and you add em to the list outside the while scope
public static List<Employees> GetAllEmployees()
{
List<Employee> listemp = new List<Employee>();
string cs = ConfigurationManager.ConnectionStrings[#"Data Source = localhost; Initial Catalog=fms; User=root; Pooling=false; Integrated Security = false"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(cs))
{
MySqlCommand cmd = new MySqlCommand("Select * from emp", con);
con.Open();
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employees em = new Employees();
em.emp_id = dr[0].ToString();
em.emp_name = dr[1].ToString();
em.u_name = dr[2].ToString();
em.pass = dr[3].ToString();
em.mail = dr[4].ToString();
em.address = dr[5].ToString();
em.city = dr[6].ToString();
em.dob = dr[7].ToString();
em.cnic = dr[8].ToString();
em.designation = dr[9].ToString();
em.ph_no = dr[10].ToString();
listemp.Add(em);
}
}
return listemp;
}
You need to return the listemp at the end of the method GetAllEmployee()
return listemp
One more thing I noticed, the listemp.Add(em) should be inside the while loop. Since your select statement will yield more than one employee. You need to add the employee object each time to the list.
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.