how can i fill the gridlookupedit correctly?.
I can not find the error.
Method fill gridlookupedit
public void CargaGLEVerdadero()
{
pcbjEntidades contexto = new pcbjEntidades();
IList consultaModeloInsumosVerdadera = (from ModeloInsumoes in contexto.ModeloInsumoes
where
ModeloInsumoes.Activo == true
select new
{
ModeloInsumoes.NombreModeloInsumo
}).ToList();
gleNombreModelo.Properties.DataSource = new BindingSource(consultaModeloInsumosVerdadera, "");
}
Construct of form
public frmAgregarMarca()
{
InitializeComponent();
CargaGLEVerdadero();
}
This issue does not related to GridLookup directly rather the to the EF/Winforms interoperation.
Since you are using DevExpress, you can use the Data Source Configuration Wizard.This feature is available for any data-aware control in threir suite and it knows how to do the things correctly and it can make all the work for you:
// This line of code is generated by Data Source Configuration Wizard
// Instantiate a new DBContext
WindowsFormsApplication2.CountriesDBEntities dbContext = new WindowsFormsApplication2.CountriesDBEntities();
// Call the Load method to get the data for the given DbSet from the database.
dbContext.Countries.Load();
// This line of code is generated by Data Source Configuration Wizard
gridLookUpEdit1.Properties.DataSource = dbContext.Countries.Local.ToBindingList();
Then you can customize Wizard's output:
dbContext.Countries.Where(c => c.Capital.StartsWith("A")).Load();
Related
I used this tutorial to build a Crystal Report in my application. The tutorial guides the user through developing an ADO.NET object and then populating it with data. I am also using a Parameter in the Crystal Report to limit the data set. The issue I'm having is that loading the report is slow and I wonder if it's because I have to load more data than I actually need before the Crystal Report employs the Parameter filter.
Following is my existing code, which runs on the report viewer form Load event. I'd like to adjust the code below to pass a parameter so I can filter the data as it's loaded rather than loading a bunch of data I know I won't need or use.
private void InspectionReportSerial2_Viewer_Load(object sender, EventArgs e)
{
International_PZD_PRDEntities db = new International_PZD_PRDEntities();
var list = db.vw_Nightly_and_Inspections.Select(p=>new {ID = p.ID,
SerialNumber = p.SerialNumber, ProductionOrder = p.ProductionOrder,
Product=p.Product, Customer=p.Customer, SalesOrder = p.SalesOrder,
LineItem = p.LineItem, Section=p.Section,InspectionPoint=p.InspectionPoint,
Status=p.Status,SectionSignoffBy = p.SectionSignoffBy,
SectionSignoffCheck=p.SectionSignoffCheck,
SectionSignoffDate = p.SectionSignoffDate,HardwareSerial=p.HardwareSerial,
SectionSortString = p.SectionSortString, ItemSortString = p.ItemSortString,
Comment = p.Comment});
InspectionReportSerial21.SetDataSource(list);
}
Add a Where. Since you are not describing the kind of filtering you need, I can only give an example:
private void InspectionReportSerial2_Viewer_Load(object sender, EventArgs e)
{
International_PZD_PRDEntities db = new International_PZD_PRDEntities();
var list = db.vw_Nightly_and_Inspections
.Where(p => p.Customer == "Dekxblock")
.Select(p => new { ID = p.ID, SerialNumber = p.SerialNumber, ... });
InspectionReportSerial21.SetDataSource(list);
}
I don't know what International_PZD_PRDEntities is. If it is some kind of O/R-mapper (e.g., EF or EF Core), then this will filter the records on the database side, which is faster than filtering the data in your application, because less records have to be transmitted. If not, then you should execute a query like SELECT * FROM vw_Nightly_and_Inspections WHERE Customer = 'Dekxblock' including the filter. Make sure to use Command.Parameters
I'm trying to retrieve the raw SQL generated by Entity Framework for the following LINQ query:
pagedItemResults = from firstItem in dbData.Accession
join secondItem in pagedRowNumberResults
on firstItem.AccessionNumber equals secondItem
select new PaginationResultRow
{
Number = firstItem.AccessionNumber,
ID = firstItem.AccessionId,
Name = firstItem.AcquisitionType.Name,
Description = firstItem.Description
};
Although it may be extremely simple and similar to the other answers already out there for previous versions of EF, I've had no luck and found nothing online.. any ideas??
You can turn on logging by implementing ILoggerProvider. See details in documentation.
You only need to register the logger with a single context instance. Once you have registered it, it will be used for all other instances of the context in the same AppDomain.
using (var db = new BloggingContext())
{
var serviceProvider = db.GetInfrastructure<IServiceProvider>();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
loggerFactory.AddProvider(new MyLoggerProvider());
}
You can also define categories what you want to log.
private static string[] _categories =
{
typeof(Microsoft.Data.Entity.Storage.Internal.RelationalCommandBuilderFactory).FullName,
typeof(Microsoft.Data.Entity.Storage.Internal.SqlServerConnection).FullName
};
You can log tsql generated to output window by :
Microsoft.Extensions.Logging.Debug
First, get it from Nuget, then in your context, you must define a LoggerFactory.
After that, use it in OnConfiguring in your context.
public static readonly Microsoft.Extensions.Logging.LoggerFactory _loggerFactory =
new LoggerFactory(new[] {
new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider()
});
optionsBuilder.UseLoggerFactory(_loggerFactory);
I really like MiniProfiler, see http://miniprofiler.com/. Short of something like this, I would say you'd have to use a profiler on the actual database.
I am using linq to sql in a windows form application. there is a bug that I couldn't find the solution until now!
partial void OnAmountChanging(int? value)
{
OrderLog lg = new OrderLog()
{
Date = DateTime.Now,
IPAddress = System.Net.Dns.GetHostAddresses(Environment.MachineName)[0].ToString(),
NewData = value,
OldData = this.Amount,
Status = "Changed",
User = User.CurUser,
Order = this // each Order has one-to-many relation to OrderLog entity.
};
}
this is run as soon as the value of AMOUNT changes in datagridview.
after closing the form I try to save the created log to Database:
db.SubmitChanges();
then I face this error :
An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported
is there any solution?
I've looked at so many posts about this, but still haven't found the solution:
I'm using a winforms app that uses EntityFramework (6?). When I load the form I can read from the DB using the context (Entities). However, when I savechanges after adding a new entity, it doesn't persist to the db.
var c = new Card { Name = tbName.Text, Quantity = int.Parse(tbQuantity.Text) };
dbContext.Cards.Add(c);
dbContext.SaveChanges();
The dbContext is setup in the form constructor and is an instance of "LiquorTrackEntities".
LiquorTrackEntities dbContext;
public Form1()
{
InitializeComponent();
dbContext = new LiquorTrackEntities()
Reading from the db works:
var cards = dbContext.Cards.ToList();
I do this stuff all the time in asp.net MVC, but it isn't working in WinForms.. is there something special I have to do in winforms? I also know about the normal "using (var db = new LiquorEntitiesEntities())" convention, but I just want to get this functioning before I worry about convention.
Any ideas?
Just tried this to no avail:
var c = new Card { Name = tbName.Text, Quantity = int.Parse(tbQuantity.Text) };
dbContext.Cards.Attach(c);
dbContext.Entry(c).State = EntityState.Added;
dbContext.SaveChanges();
Just tried creating a new EDMX using EF5 instead.. same problem.
UPDATE:
SaveChanges does return a 1 when after adding a card. It stays in the context (if I reload my cards from the context, the new one is there..) but never makes it to the database.
So, here is my hopefully unique spin on this common problem.
I do my query, get my objects then pass the object into a form where it populates the form with the data from the object (this is not passed in by reference).
I then edit the values of the object that was queried (via the form) and then return a new object constructed from the values in the form.
I then want to update this to the database. Attach does nothing (runs but does not update). SubmitChanges also does nothing (and both do nothing when used together).
What am I missing?
Update: here is the code I am using:
// In constructor
_dataMap = new DataMapDataContext();
_addresses = _dataMap.AddressItems
.Where(address => address.InsertUserName == _currentUser.Name).ToList();
public void EditButtonClick()
{
using (AddAddressForm form = new AddAddressForm(_addresses[_currentAddress]))
{
form.Text = "Edit Address";
if (DialogResult.OK == form.ShowDialog())
{
_addresses[_currentAddress] = form.Item;
_dataMap.SubmitChanges();
DisplayItem();
}
}
}
You'll need to get the record from the database, update it's values and then call SubmitChanges()
using(MyDataContext db = new MyDataContext())
{
// get the record
Product dbProduct = db.Products.Single(p => p.ID == 1);
// set new values
dbProduct.Quantity = 5;
dbProduct.IsAvailable = false;
// save them back to the database
db.SubmitChanges();
}
Turns out I was doing almost everything right.
I just needed to pass in the object I was editing by reference. That way when it got changed, it was not a new object that was returned, but the same one (that Linq-to-SQL already knew about.)
These are the two lines from the code above that got changed:
AddressItem itemToEdit = _addresses[_currentAddress];
using (AddAddressForm form = new AddAddressForm(ref itemToEdit))