Please tell me. Created 2 classes (Data Model)
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Department { get; set; }
public int Office { get; set; }
public string Position { get; set; }
public string Phone { get; set; }
public float Mobile { get; set; }
public string EMail { get; set; }
public string Login { get; set; }
public int idArm { get; set; }
}
and
public class arm
{
public int id { get; set; }
public string name { get; set; }
public string Detalis { get; set; }
}
I installed 2 GridControlls on the form
And through DataSet showed data
string connectionString = ConfigurationManager.ConnectionStrings["connectionSIPiT"].ConnectionString;
string command = "SELECT * FROM Users";
string command2 = "SELECT * FROM arm";
sqlConnection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(command2, sqlConnection);
SqlDataAdapter adapter1 = new SqlDataAdapter(command, sqlConnection);
DataSet dataset1 = new DataSet();
adapter.Fill(dataset1, "arm");
adapter1.Fill(dataset1, "Users");
DataColumn keyColumn = dataset1.Tables[0].Columns[0];
DataColumn foreignKeyColumn = dataset1.Tables[1].Columns[9];
dataset1.Relations.Add("armUsers", keyColumn, foreignKeyColumn);
armBindingSource.DataSource = dataset1;
armBindingSource.DataMember = "arm";
userBindingSource.DataSource = armBindingSource;
userBindingSource.DataMember = "armUsers";
gridControl1.DataSource = userBindingSource;
gridControl2.DataSource = armBindingSource;
How do I select a row in the main table GridControll. Send report data. Or pass the id of the main table to build the report? Can anyone come across such a task?
Make sure that the Modifiers property for the report parameter is set to Public or Internal
Use the GridView.GetRowCellValue method to get the ID column value of the focused record
The following assumes that you have a report called MyReport and it has a parameter called MyParameter.
var id = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["UserID"]));
var rpt = new MyNewReport();
rpt.MyParameter.Value = id; //Make sure the MyParameter's Modifiers property is set to Public or Internal.
Related
public List<Customer> getCustomer()
{
using (IDbConnection con=DapperConnection())
{
string sql = "Select * from Customer";
return con.Query<Customer>(sql).Select(x => new { x.Id, x.LastName })
.ToList();
}
}
class Customer
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set;}
}
Does anyone know how to return specific columns using dapper? What I am trying to achieve is to return just the Id and LastName as List so that I can bind them to my controls.
Unsure exactly what you mean but surely you should return the customer object instead of an anonymous type, or at least make a smaller version of the customer object to be used by the controls
public List<Customer> getCustomers()
{
using (IDbConnection con = DapperConnection())
{
string sql = "Select * from Customer";
return con.Query<Customer>(sql).ToList();
}
}
Or if you dont like the overhead of returning the full customer object
public List<CustomerBase> getCustomers()
{
using (IDbConnection con = DapperConnection())
{
string sql = "Select * from Customer";
return con.Query<CustomerBase>(sql).ToList();
}
}
public class CustomerBase
{
public string Id { get; set; }
public string LastName { get; set; }
}
public class Customer: CustomerBase
{
public string FirstName { get; set; }
//Other props...
}
Using basic DAL 2 to grab data from a table that has tabid. Would like to also get the Tab Url through the DNN API. I could join to the Tabs table, but want to work with the api.
Here is my model.
[TableName("My_Products")]
[PrimaryKey("ProductId")]
[Cacheable("My_Products_", CacheItemPriority.Normal, 20)]
public class ProductInfo
{
public ProductInfo()
{
Langs = new List<ProductLangInfo>();
}
public int ProductId { get; set; }
[ReadOnlyColumn]
public string ProductImage { get; set; }
public int LineID { get; set; }
[ReadOnlyColumn]
public string Culture { get; set; }
[ReadOnlyColumn]
public string ProductName { get; set; }
[ReadOnlyColumn]
public string ProductShortDesc { get; set; }
[ReadOnlyColumn]
public int TabId { get; set; }
[ReadOnlyColumn]
public string ProductURL { get; set; } //GET THIS FROM API
[ReadOnlyColumn]
public List<ProductLangInfo> Langs { get; set; }
}
This is my Controller
public IEnumerable<ProductInfo> GetProducts(string language)
{
using (IDataContext ctx = DataContext.Instance())
{
string sqlCmd = ";WITH cte as (SELECT * FROM [ProductsLang] WHERE Culture = #0)" +
" SELECT Products.*,cte.ProductName, cte.ProductShortDesc, cte.TabId" +
" FROM [Products] as Products" +
" INNER JOIN cte ON Products.ProductId = cte.ProductId";
string order = " ORDER BY Products.ProductId DESC";
return ctx.ExecuteQuery<ProductInfo>(CommandType.Text, sqlCmd + order, language);
}
}
I guess my question is where is the best way to pass in the tabid from my query to the DNN API?
This should be fairly straightforward using the NavigateURL method off DotNetNuke.Common.Globals
var url = DotNetNuke.Common.Globals.NavigateURL(TabId);
There are other ways to get URLs in DNN, but that is the easiest way that should respect all the various Providers that can be used to build out a URL
private string _productUrl = '';
public string ProductURL { get return DotnetNuke.Common.Globals.NavigateURL(TabId); }
I am developing a web api using Entity Framework 6. I have to execute a complex SQL query which is getting data from multiple tables as shown in the code. I have tried but get the following error:
The data reader has more than one field. Multiple fields are not valid
for EDM primitive or enumeration types.
The query successfully return data in SSMS Query Analyzer.
[HttpGet]
public IHttpActionResult getJobNo(string cmpCode, string branchCode)
{
string query = string.Format(
#"select
M.S1, M.S2, C.S2 As S30, M.S3, SC.S2 As S31, M.D1, M.S5,
JT.S2 As S32, M.S6, TM.S2 As S33, M.S10
from
MERTRM M, CMF C, CMFS SC, PMCD JT, PMCD TM
where
M.S100 = 'JOB' and M.S102 = '{0}' and
M.S108 = '{1}' and
M.S101 not in('U', 'C', 'F') and
M.S2 = C.S1 and C.S102 = '{0}' and
C.S100 = 'CC' and M.S3 = SC.S1 and SC.S102 = '{0}' and
C.S1 = SC.S109 and M.S5 = JT.S1 and JT.S102 = '{0}' and
JT.S100 = 'JTP' and M.S6 = TM.S1 and TM.S102 = '{0}' and
TM.S100 = 'TPM'",
cmpCode,branchCode);
var result = db.Database.SqlQuery<string>(query);
return Json(result.ToArray());
}
Since the query returns a list of records so when I tried as follows:
var result = db.Database.SqlQuery<IEnumerable<string>>(query).ToList();
It gave me the following error:
'System.Collections.Generic.IEnumerable`1[System.String]' may not be
abstract and must include a default constructor.
How can I execute this query?
Regards!
you must use a DAO/DTO type:
class MyDAOType {
public String S1 {get; set;}
public String S2 {get; set;}
//...
public String S10 {get; set;}
}
and the query
var result = db.Database.SqlQuery<MyDAOType>(query);
Probably the easiest way is to define your own class that has the same fields as returned SQL and use this class as output.
public class MyReport {
public string S1 { get; set; }
public string S2 { get; set; }
public string S30 { get; set; }
public string S3 { get; set; }
public string S2 { get; set; }
public string S31 { get; set; }
public string D1 { get; set; }
public string S5 { get; set; }
public string S32 { get; set; }
public string S6 { get; set; }
public string S33 { get; set; }
public string S10 { get; set; }
}
var result = db.Database.SqlQuery<MyReport>(query).ToList();
I got the error Invalid object name 'dbo.Staffs'. but I'm not sure why. I actually deleted and recreated my database with EF because previously I had other errors. But I'm quite sure I recreated it correctly because I've done it in the same way for other programs and it works fine.
.edmx database diagram
Controller
private StaffPortalDBEntities1 db = new StaffPortalDBEntities1();
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["StaffPortalDBConnectionString"].ConnectionString);
[Authorize]
public ActionResult Index()
{
var userEmail = User.Identity.Name;
var model = db.Staffs.Where(i => i.Email == userEmail).Include("Histories").Include("CurrentApplications").FirstOrDefault();
return View(model);
}
I got the error is for the line var model = db.Staffs.Where(i => i.Email == userEmail).Include("Histories").Include("CurrentApplications").FirstOrDefault();
Generated Staff class
public partial class Staff
{
public Staff()
{
this.Histories = new HashSet<History>();
this.CurrentApplications = new HashSet<CurrentApplication>();
}
public int StaffID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Nullable<decimal> AllocatedLeave { get; set; }
public Nullable<int> BalanceLeave { get; set; }
public virtual ICollection<History> Histories { get; set; }
public virtual ICollection<CurrentApplication> CurrentApplications { get; set; }
}
Try this:
var model = db.Staffs.Where(i => i.Email == userEmail).Include(x=>x.Histories).Include(x=>x.CurrentApplications).FirstOrDefault();
i have a lot of some WPF windows. On them i write some code, which binds UI controls and data, something like this:
public class AddressWindow
{
public string AddressID { get; set; }
public string Addr1 { get; set; }
public string Addr2 { get; set; }
public string ZIP { get; set; }
public string City { get; set; }
public string Mobile { get; set; }
public string FAX { get; set; }
public string Country { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public bool IsSystem { get; set; }
public bool Enabled { get; set; }
}
private void BindInCode()
{
var address = new AddressWindow
{
// AddressID = "110",
// Addr1 = "Kaunas",
// Addr2 = "Jonavos",
// ZIP = "8987",
// City = "miestas",
// Mobile = "869985868",
// FAX = "87998",
// Country = "Lithuania",
// Email = "emailas#ree.lt",
// Phone = "37598288",
// IsSystem = true,
// Enabled = false
};
Binding binding = new Binding();
binding.Source = address;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
binding.Path = new PropertyPath("AddressID");
this.db_AddressID.SetBinding(TextEdit.TextProperty, binding);
binding = new Binding();
binding.Source = address;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
binding.Path = new PropertyPath("Addr1");
this.db_Addr1.SetBinding(TextEdit.TextProperty, binding);
...............
}
Now I want to create some universal simple engine, to fill Data values(assign values from DataTable to my class properties). Does anyone knows some way how to do that..? In example is there some way to assign values by names of properties. Lets say my property names equals to names of Columns from dataRow. Is its possible in wpf to do something like that, or not, should i assign values in every window manually?
Personally I've recourse to code generation to do the job, but you can use to Jimmy Bogard's AutoMapper, which does pretty much what you're talking about. It is on CodePlex and GoogleCode.