how to use Utils.FormIsOpen in c# - c#

I need help using Utils in C#.
private void manageUsersToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!Utils.FormIsOpen("ManageUsers"))// this line gives an error
{
var manageUsers = new ManageUsers();
manageUsers.MdiParent = this;
manageUsers.Show();
}
}
I am using Microsoft SQL Server Management Studio 18 along with visual studio and I am trying to manage my users in the database while not physically adding them to the database but adding them at runtime.

It seems that there is no FormIsOpen method in Utils Class.
If you want to check if a "ManageUsers" has been opened, you can call Form.MdiChildren Property to get all mdichildforms and filter them.
// get all child form
Form[] children = this.MdiChildren;
var query = children.Select(c => c)
.Where(c => c is ManageUsers).ToList();
if(query.Count == 0)
{
var manageUsers = new ManageUsers();
manageUsers.MdiParent = this;
manageUsers.Show();
}

Related

Filter Record Set Before Populating ADO.NET Object

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

C# SubmitChanges dont work

I'm trying to make a database. I made a form and I want to get the data from that. Everything seems okay, but my database didn't get the update and didn't change. I'm using a local database. I tried to change "Copy to output directory" but nothing happened... The SubmitChanges seems ok: I get the Message, but after the program is closed the database doesn't contain the new data... I saw a lot of posts with similar problems, but I didn't find a solution.
My Code:
private void felvetButton_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
if (f2.ShowDialog() == DialogResult.OK)
{
Match m = new Match();
try
{
m.datum = f2.datumTextBox.Text;
m.mod_fk = Convert.ToInt32(f2.modTextBox.Text);
m.acc_fk = Convert.ToInt32(f2.accTextBox.Text);
m.champion = f2.champTextBox.Text;
m.szerep_fk = Convert.ToInt32(f2.roleTextBox.Text);
m.kimenet = f2.resultTextBox.Text;
m.mhossz = f2.lengthTextBox.Text;
m.kill = Convert.ToInt32(f2.killTextBox.Text);
m.death = Convert.ToInt32(f2.deathTextBox.Text);
m.assist = Convert.ToInt32(f2.assistTextBox.Text);
m.kda = float.Parse(f2.kdaTextBox.Text);
m.killpart = float.Parse(f2.kpTextBox.Text);
m.farm = Convert.ToInt32(f2.farmTextBox.Text);
m.ward = Convert.ToInt32(f2.wardTextBox.Text);
m.redward = Convert.ToInt32(f2.redwTextBox.Text);
db.SubmitChanges();
MessageBox.Show("New match added to the database!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Don't you have to make the entity layer aware of this new object? I imagine you need to call something like this for new entities:
db.Matches.InsertOnSubmit(match);
Its perfectly fine to load entities from the database via this entity layer, modify them, and then use db.SubmitChanges() because the entity layer already knows about existing objects! How is it supposed to know about this new object if the entity layer doesn't have any reference to it?

PublishOptions failing when trying to publish media items programatically

I'm trying to publish a media item programmatically but I am encountering a problem with the PublishOptions object. At runtime, the line of code where the PublishOptions object is being instantiated is breaking when I upload a media item. I get the following message saying:
"Value cannot be null. Parameter Name: item"
And the stacktrace is:
at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
at Sitecore.Data.Managers.ItemProvider.ApplySecurity(Item item, SecurityCheck securityCheck)
at Sitecore.Data.Managers.ItemProvider.GetRootItem(Language language, Version version, Database database, SecurityCheck securityCheck)
at Sitecore.Data.Managers.ItemManager.GetRootItem(Language language, Version version, Database database)
at Sitecore.Nexus.Data.DataCommands.ResolvePathCommand.(String itemPath, Database database)
at Sitecore.Nexus.Data.DataCommands.ResolvePathCommand.(String itemPath, Database database)
at Sitecore.Nexus.Data.NexusDataApi.ResolvePath(String itemPath, Database database)
at Sitecore.Data.Engines.DataCommands.ResolvePathCommand.DoExecute()
at Sitecore.Data.Engines.EngineCommand`2.Execute()
at Sitecore.Data.Engines.DataEngine.ResolvePath(String itemPath)
at Sitecore.Data.Managers.ItemProvider.ResolvePath(String itemPath, Database database)
at Sitecore.Data.Managers.ItemProvider.GetItem(String itemPath, Language language, Version version, Database database, SecurityCheck
Here's the code where the PublishOptions object is being instantiated (where it is breaking):
public void OnItemSaved(Object sender, EventArgs args)
{
var item = Event.ExtractParameter(args, 0) as Item;
using (new SecurityDisabler())
{
if (item != null)
{
if (item.Paths.IsMediaItem)
{
var source = new Database("master");
var target = new Database("web");
var options = new PublishOptions(source, target, PublishMode.SingleItem, item.Language, DateTime.Now)
{
RootItem = item,
Deep = true,
};
var publisher = new Publisher(options);
publisher.PublishAsync();
}
}
}
}
What could be the reason why I'm getting this error?
Try this and see if this works, I am guessing it is to with the database.
public void OnItemSaved(Object sender, EventArgs args)
{
var item = Event.ExtractParameter(args, 0) as Item;
using (new SecurityDisabler())
{
if (item != null)
{
if (item.Paths.IsMediaItem)
{
var source = Factory.GetDatabase("master");
var target = Factory.GetDatabase("web");
var options = new PublishOptions(source, target,
PublishMode.SingleItem, item.Language,
DateTime.Now)
{
RootItem = item,
Deep = true,
};
var publisher = new Publisher(options);
publisher.PublishAsync();
}
}
}
}
I think you're being caught in a classic mistake. item:saved will also fire for your items as they're being published.
My initial guess would be, that you should expand your check to be
if ( item != null && item.Database.Name == "master" )
to prevent your code from attempting to publish the item, as the item:saved event fires on "web" during publishing.

Convert a BsonDocument into a String

I have a query that needs to return a product and all its information by its "_id" but when I print the results I get the cursor and not the values in the MongoDB collection?
var query_id = dbCollection.Find(Query.EQ("_id", "50ed4ea05baffd13a44d0154"));
return query_id.ToString();
The result is
MongoDB.Driver.MongoCursor`1[MongoDB.Bson.BsonDocument]
EDIT
I have tried the .ToJson() method but that only returns []
var query_id = dbCollection.Find(Query.EQ("_id", "50ed4ea05baffd13a44d0154"));
var r = query_id.ToJson();
EDIT
Here is the Code, its a basic Windows Form App that I am doing basic tests with:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "mongodb://127.0.0.1";
var server = MongoServer.Create(connectionString);
if (server.State == MongoServerState.Disconnected)
{
server.Connect();
label1.Text = ("Server is connected to: " + connectionString);
button1.Enabled = false;
}
else
{
label1.Text = ("Server Error Check DB location");
}
var conn = server.GetDatabase("Acord");
var dbCollection = conn.GetCollection("Mappings");
var query = dbCollection.FindAll();
//textBox2.Text = query.ToList().ToJson();
var query_first = query.First();
//textBox2.Text = cursorResult.ToString();
var query_id = Query.EQ("_id", "1");//What ID do I put in here? This is were the error is now!
var entity = dbCollection.FindOne(query_id);
textBox2.Text = entity.ToString();
}
}
Here is the error I get:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe
I am clearly missing something here?
As you've noticed, Find() returns a cursor, which could be used to traverse the query results one-by-one. But you need only a single returned item, so use FindOne() - it will return you an item instead of a cursor.
You should also use the correct query. E.g. By-Default MongoDB uses internal 12-bytes ObjectID, so Query.EQ("_id", "1") is simply wrong. If you are not sure about the query, call FindOne() without any arguments.
I would recommend you trying the online MongoDB shell (at http://mongodb.org) which contains an interactive MongoDB tutorial.

Permissions error in C# application, System.Net.NetworkInformation?

In folder AppCode MyUrl.class
C#
MyUri a = new MyUri();
private Fun1()
{
a.SelectFromDB(); // This function errors out. (see below)
}
DataClassesDataContext dc = new DataClassesDataContext();
private fun2()
{
// This code works fine.
var a = from v in dc.DB
select v;
}
What does the following error mean?
request for the permission of type system.net.networkinformation
permission system version=2.0.0.0 culture=neutral,publickkey
Token=b77a5c561934e089
You have two different functions
MyUri a = new MyUri();
private Fun1()
{
a.SelectFromDB();
// This function errors out. (see below)
}
DataClassesDataContext dc = new DataClassesDataContext();
private fun2()
{
// This code works fine.
var a = from v in dc.DB
select v;
}
Seems both doesnt hv any relationship. Also SelectFromDB method is doing what. What type of class is MyUri. Paste some code of MyUri.
I am wondering if you are not running into an issue with your LINQ query.
See the following MS thread for some strange LINQ behavior:
how to make a query in linq to data services

Categories