ASP.Net MVC Code for MySQL Server
Read data from database
Code
public ActionResult Index()
{
List<CustomerModel> customers = new List<CustomerModel>();
string connstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(connstr))
{
string query = "SELECT id,fullname, username FROM tbUser";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new CustomerModel
{
id = Convert.ToInt32(sdr["id"]),
fullname = sdr["fullname"].ToString(),
username = sdr["username"].ToString()
});
}
}
con.Close();
}
}
return View(customers);
}
Picture
Example 2
public ActionResult Index()
{
List<Customer> customers = new List<Customer>();
string connstr = ConfigurationManager.ConnectionStrings["conndbstr"].ConnectionString;
using (SqlConnection con = new SqlConnection(connstr))
{
string query = "SELECT CusID,FullName,NickName,Company,Phone,Fax,Mobile,Email,Address,TaxReg,Branch,Remark,Status FROM Customer";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new Customer
{
CusID = sdr["CusID"].ToString(),
FullName = sdr["FullName"].ToString(),
NickName = sdr["NickName"].ToString(),
Company = sdr["Company"].ToString(),
Phone = sdr["Phone"].ToString(),
Fax = sdr["Fax"].ToString(),
Mobile = sdr["Mobile"].ToString(),
Email = sdr["Email"].ToString(),
Address = sdr["Address"].ToString()
});
}
}
con.Close();
}
}
return View(customers);
}
Picture
Insert to database
Code
public ActionResult Create(CustomerModel users)
{
//List<CustomerModel> users = new List<CustomerModel>();
string connstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(connstr))
{
string query = "INSERT INTO tbUser(fullname,username,password,Role) VALUES(@fullname,@username,@password,@Role)";
using (MySqlCommand cmd = new MySqlCommand(query))
{
bool status = false;
if (ModelState.IsValid)
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@fullname", users.fullname);
cmd.Parameters.AddWithValue("@username", users.username);
cmd.Parameters.AddWithValue("@password", EncryptString.Encrypt.EncryptText(users.password));
cmd.Parameters.AddWithValue("@Role", users.Role);
users.id = Convert.ToInt32(cmd.ExecuteNonQuery());
con.Close();
status = true;
}
}
}
return View(users);
}
Picture
Delete from database
Code
public ActionResult DeleteConfirmed(string id)
{
List<CustomerModel> users = new List<CustomerModel>();
string connstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(connstr))
{
using (MySqlCommand cmd = new MySqlCommand("DELETE FROM tbUser WHERE id = @id"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
bool status = false;
cmd.Parameters.AddWithValue("@id", id);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
status = true;
}
}
}
//tbInvoice tbInvoice = db.tbInvoice.Find(id);
//db.tbInvoice.Remove(tbInvoice);
//db.SaveChanges();
return RedirectToAction("Index");
}
Picture
This blogger for collected the tip and technical about programming code and how to solve a problem by yourself when you have problem with you personal computer.And I have try to collect code and any technical for find when have a problem again. บล็อกนี้เขียนขึ้นไว้สำหรับแก้ปัญหาคอมพิวเตอร์เบื้องต้นซึ่งผมได้รวบรวม ข้อมูลเทคนิค หรือวิธีการต่างๆ จากประสบการณ์ และจากการค้นข้อมูลในเวปไซต์ และเพื่อเก็บข้อมูลไว้ดูเอง เมื่อเกิดปัญหาอีก
Monday, October 16, 2017
Wednesday, May 10, 2017
Exit button and message box
Private Sub btExit_Click(sender As Object, e As EventArgs) Handles pbExit.Click
If MsgBox("Do you want to quit?", MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Question, "Close application") = Windows.Forms.DialogResult.Yes Then
Application.Exit()
End If
End Sub
Friday, February 3, 2017
Crystal Report Cannot Load in VS2013
Crystal Report Error Message
'Could not load file or assembly 'file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb _adoplus.d ll' or one of its dependencies. The system cannot find the file specified.'
How to Fix it:
Add this to your App.config file:
<startup useLegacyV2RuntimeActivationPolicy="true" >

'Could not load file or assembly 'file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb
How to Fix it:
Add this to your App.config file:
<startup useLegacyV2RuntimeActivationPolicy="true" >

Subscribe to:
Comments (Atom)







