James
James

Reputation: 83

Dynamic sql query in webmatrix in 2 different tables

I am pulling an order ID from a previous page to select a specific order from a table and that works fine. I also need to get the CustomerID from that table and query the customer table to return the entry with that ID.

@{
var id=Request["id"];
var SQLSELECT3 = "SELECT * FROM Ordr WHERE ID=@0";
var db = Database.Open("OMD");
var ordr = db.QuerySingle(SQLSELECT3,id);
var oCustomerID=ordr.CustomerID;
var oStatus=ordr.Status;
var oID=ordr.ID;
var oDate=ordr.Date;
var oService=ordr.Service;
var oNotes=ordr.Notes;

var SQLSELECT = "SELECT * FROM Customer WHERE ID = @oCustomerID";
var customer = db.QuerySingle(SQLSELECT,id);
var cLastName=customer.LastName;
var cFirstName=customer.FirstName;
var cAddress=customer.Address;
var cCity=customer.City;
var cState=customer.State;
var cZip=customer.Zip;
var cPhone1=customer.Phone1;
var cPhone2=customer.Phone2;
var cEmail=customer.Email;
}

Upvotes: 1

Views: 219

Answers (1)

Mike Brind
Mike Brind

Reputation: 30110

Change your query to

var SQLSELECT = "SELECT * FROM Customer WHERE ID = @0";
var customer = db.QuerySingle(SQLSELECT, (int)ordr.CustomerID);

Nothing to do with your issue, but I don't understand why you transfer all the values you get from the query to another set of variables. That's a huge amount of unnecessary typing, in my opinion.

Upvotes: 1

Related Questions