|
Well that's really silly isn't it?
I've never once bumped a thread. I got no replies so I figured it was overlooked.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Did you ever consider that no one has the answer?
|
|
|
|
|
Yes, I considered that
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Was up against the notorious, apostrophe issue, when sending a command line, which included an apostrophe, to a Database.
So after an enormous amount of Google'ing, I see now that it is recommended procedure to use parameterized commands, to do the trapping for you.
So I did just that.
Rewrote my code with the parameterized commands.
Simply put, my code is designed to read all file names off of a drives root, and enter them into a Database.
As you can see in my code, I store all of the file names into an Array, and then retrieve them one by one using a, FOREACH.
All well and good, and it does work, Line 36,
Console.WriteLine(fi.Name); proves it, but, Line 31,
cmd.Parameters.AddWithValue("@FileName", fi.Name); only grabs the very first filename, and then enters that first filename, as many times into the Database, as there are files on the root directory.
Why is Line 36, the Console Window line working perfectly, reflecting every single filename, but Line 31 only grabs the very first filename, and holds on to it?
static void Main(string[] args)
{
DriveInfo di = new DriveInfo(@"C:\");
string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=e:\\mydbtry.accdb";
OleDbConnection con = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
con.ConnectionString = conString;
con.Open();
DirectoryInfo dirInfo = di.RootDirectory;
FileInfo[] fileNames = dirInfo.GetFiles("*.*");
foreach (FileInfo fi in fileNames)
{
cmd.CommandText = "Insert into Table1 (FileName) Values (@FileName)";
cmd.Parameters.AddWithValue("@FileName", fi.Name);
cmd.Connection = con;
cmd.ExecuteNonQuery();
Console.WriteLine(fi.Name);
}
con.Close();
Console.WriteLine("Finished.");
Console.ReadKey();
}
|
|
|
|
|
You're continuously adding more parameters to the OleDbCommand.Parameters -collection when you only ever need a single one. The first one you add is being used for every command execution.
Store the parameter in a variable, add it to the parameters-collection only once and then modify its value instead in the loop.
Also, I would recommend you to use all your OleDb*-objects that are disposable, in a using-block:
using (OleDbConnection con = new OleDbConnection())
using (OleDbCommand cmd = new OleDbCommand())
{
}
That's the cleanest way to ensure you're not leaking resources. Goes for everything else that implements IDiposable as well, not only database-objects.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Try something like:
cmd.Connection = con;
cmd.CommandText = "Insert into Table1 (FileName) Values (@FileName)";
SqlParameter param = new SqlParameter("@FileName", SqlDbType.NVarChar);
cmd.Parameters.Add(param);
foreach (FileInfo fi in fileNames)
{
param.Value = fi.Name;
if (cmd.ExecuteNonQuery() < 1)
{
}
Console.WriteLine(fi.Name);
}
|
|
|
|
|
Sascha, Richard,
You know, in C# there are at least twenty different ways to achieve the same goal.
And that's why you'll never see two programmers shaking hands in agreeance on the same approach.
Having said that, I want to thank both of you. Although coming from different directions, both of your assistance were correct. Thank you.
|
|
|
|
|
I have an application which displays a treeview based on data contained in a dedicated file.
I would like to get at the same time the name, index, ... of the current clicked treeview item in order to give additional details on click event.
I have been looking in different forums but all I've seen there are somewhat complicated approaches not that easy to adapt to my application.
Has anyone developed a project for this purpose?
Thanks in advance for your replies.
RV
|
|
|
|
|
|
Thank you for your reply Eddy.
Actually I had already tried this approach but I cannot get the "System.Windows.Forms" using directive not grayed. The event on clicked item method is likned to SelectedItemChanged PropertyThis might be for you a basic issue to deal with?
Consequently I get an error message about a missing definition for "SelectedNode" property.
Here is my method:
private void StateTreeView_SelectedItem(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//SelectedItemName.Text = Convert.ToString(StateTreeView.SelectedItem);
SelectedItemName.Text = StateTreeView.SelectedNode;
}
|
|
|
|
|
Member 13511312 wrote: I cannot get the "System.Windows.Forms" using directive not grayed. If it is gray that means it is not used.
Member 13511312 wrote: The event on clicked item method is likned to SelectedItemChanged PropertyThis might be for you a basic issue to deal with? Not really; only difference is that it fires when the selected node changes, instead of a mouse-click.
Member 13511312 wrote: private void StateTreeView_SelectedItem(object sender, RoutedPropertyChangedEventArgs<object> e) ..ah, I was assuming the Windows Forms treeview. Seems like you are using WPF.
You don't want to mix those two if you are a beginning programmer. I don't have much experience with WPF, but there's an article on exactly that specific subject here[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thank you for your help.
I will give a look to that project and see what could be useful for my application.
Best regards,
RV
|
|
|
|
|
If you want the "name" of "SelectedNode", then it's:
StateTreeView.SelectedNode.Name
(Name is a "property" of SelectedNode).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I have opened a project of mine and promptly stumbled over an old sin: A virtual method call in a constructor. The project involves a DIY UI that runs in a 3D engine and the virtual method call is needed in the constructor of the controls.
It works like a charm, but calling a virtual method in a constructor may cause trouble and I want to get rid of the warning. If you want to take a look: Video clickbait[^]
The problem is that I can't find a way around that virtual method when initializing the properties of a control. I can only do that in the constructor because the view layout is loaded from XAML, like this:
<cLoginView Id="LoginView" Width="405" Height="83" DockStyle="ADAPT"
xmlns="clr-namespace:FoC.MemPraeUserModule.Presentation;assembly=MemPraeUserModule"
xmlns:foc="clr-namespace:FoC.Praetor4UI.Controls;assembly=Praetor4UI"
xmlns:fpd="clr-namespace:FoC.Praetor4UI.DataObjects;assembly=Praetor4UI"
xmlns:fpt="clr-namespace:FoC.Praetor4UI.Theme;assembly=Praetor4UI"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<foc:cPraeLabel Id="LblLoginName" PositionX="5" PositionY="17" Width="75" Height="20" Text="Login Name:" />
<foc:cPraeTextbox Id="TbLoginName" PositionX="85" PositionY="15" Width="313" Height="25" />
<foc:cPraeImageButton Id="BtnListAccounts" Image="Button_128" PositionX="270" PositionY="45" Width="128" Height="22" Text="List My Accounts" LeftButtonClicked="BtnListAccounts_LeftButtonClicked"/>
<foc:cPraeListview Id="LvServers" Style="ServerList" PositionX="85" PositionY="85" Width="313" Height="150" />
<foc:cPraeLabel Id="LblPasswd" PositionX="5" PositionY="242" Width="75" Height="20" Text="Password:" />
<foc:cPraeTextbox Id="TbPasswd" PositionX="85" PositionY="240" Width="313" Height="25" PasswordCharacter="x" />
<foc:cPraeImageButton Id="BtnLogin" Image="Button_128" PositionX="137" PositionY="275" Width="128" Height="22" Text="Login" LeftButtonClicked="BtnLogin_LeftButtonClicked"/>
<foc:cPraeImageButton Id="BtnCancel" Image="Button_128" PositionX="270" PositionY="275" Width="128" Height="22" Text="Cancel" LeftButtonClicked="BtnCancel_LeftButtonClicked"/>
</cLoginView>
When a control is loaded this way, the constructor must do the following things before the properties are set by the XAML parser:
1) Set the default value for each property
2) Set the values from a control style, if one has been defined
3) Set the values from a named style, if one has been set for this control
The constructor of the base is simple enough:
public cPraeControl()
{
m_oUserInterface = null;
InitializeDefaultProperties();
ControlStyleChanged(cPraeUserInterface.CurrentTheme);
}
Calling InitializeDefaultProperties() ensures that the whole procedure is done for all properties of the topmost derived class, including the inherited ones. The derived controls have no own constructors that could interfere. That's why this works perfectly. I just don't see a way to get around calling InitializeDefaultProperties().
================================================================================
Edit:
I think I got it. It took only a few hours of refactoring, headscratching and debugging. The virtual method call (plus some hidden ones) are gone. Now every control has a proper constructor, like this:
public cPraeTextbox()
{
InitializeDefaultProperties();
SetStyle(GetControlStyle(cPraeUserInterface.CurrentTheme));
}
InitializeDefaultProperties() is now a normal private method that simply sets the default values for the properties, just as SetStyle() now only takes the for the class relevant values from the style object.
The magic happens in GetControlStyle(), which has the hard job of figuring out which styles have to be applied. Thank god for reflection! Since each class in the hierarchy now can find what it needs, there is no more need for constructing the controls top down (= no more virtual calls).
Now I have one control left that does not look right, but that will probably only take some more more debugging.
Thanks for the moral support. I knew you were all with me. Silently.
I have lived with several Zen masters - all of them were cats.
modified 26-Nov-17 9:57am.
|
|
|
|
|
Hi everybody,
Could someone help me with the issue mentionned in the title?
I get NULLReference exception at line "EqtLevel2[] eqtlevel2 = EqtListDataBase.GetSetOfEqtLevel2(eqtlevel1[i]);" when running the code below.
Thank you for your answers.
RV
public TreeViewItem BuildTreeView ()
{
TreeViewItem TreeView = new TreeViewItem();
EqtLevel2[] eqtlevel2;
//EqtLevel3[] eqtlevel3;
EqtLevel1[] eqtlevel1 = EqtListDataBase.GetSetOfEqtLevel1(_eqtlist);
for (int i = 0; i < eqtlevel1.Length; i++)
{
TreeViewItem ParentItem = new TreeViewItem() { Header = eqtlevel1[i].EqtLevel1Name };
EqtLevel2[] eqtlevel2 = EqtListDataBase.GetSetOfEqtLevel2(eqtlevel1[i]);
for (int j = 0; j < eqtlevel2.Length; j++)
{
/*TreeViewItem Child2Item = new TreeViewItem() { Header =
eqtlevel2[j].EqtLevel2Name };
eqtlevel3 = EqtListDataBase.GetSetOfEqtLevel3(eqtlevel2[j]);
for (int k = 0; k < eqtlevel3.Length; k++)
{
TreeViewItem Child3Item = new TreeViewItem() { Header =
eqtlevel3[k].EqtLevel3Name};
Child2Item.Items.Add(Child3Item);
}
ParentItem.Items.Add(Child2Item);
*/
}
TreeView.Items.Add(ParentItem);
}
return TreeView;
}
|
|
|
|
|
Use your debugger. I'm going to guess that your index is out of range, and that there is no data at eqtlevel1[i].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Than you for your reply Eddy.
I will try to provide additional data concerning the issue I'm dealing with.
I have a root List called "eqtlist" which is composed of items called eqtlist1. Each eqtlist1 contains itself another List (eqtlist2). And each eqtlist 2 is composed of items called eqtlist3.
The idea is to build a treeview containing the details of that implementation. For that purpose, I need to access individually each item of that collection in order to get its name which will be displayed as an item of the treeview.
All this information is stored in a library called EqtListDataBase. My code allows me to access the first (eqtlist) and the second (eqtlevel1) levels. But when trying to go further, I get the NullReference error.
|
|
|
|
|
Member 13511312 wrote: But when trying to go further, I get the NullReference error. Which you need to investigate; one of those objects you are referencing on that line does not exist. Meaning it results in "null" and not in an object you expect.
Taking the usual suspects, "EqtListDataBase" could throw such an exception, but is probably initialized correctly, otherwise it would have crashed sooner. If the length of the array was incorrect, is would have crashed on the previous line.
What is the method called "GetSetOfEqtLevel2" returning? An array?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, GetSetOfEqtLevel2 returns an array of eqtlevel2.
By running step by step the application in the console mode, I've pointed out that the issue comes from the eqtlevel2 items which dont have eqtlevel3 elements.
Lets consider the following example:
eqtlist: 2 items of type eqtlevel1 {eqtlevel1a and eqtlevel1b}
eqtlevel1a: 3 items of type eqtlevel2 {1a.eqtlevel2.1; 1a.eqtlevel2.2 and 1a.eqtlevel2.3}
1a.eqtlevel2.1: 1 item of type eqtlevel3 {1a.2.1.eqtlevel3}
eqtlevel1b: 1 item of type eqtlevel2 {1b.eqtlevel2.1}
In such a case the application will display the NullReference error when trying to get objects of type eqtlevel3 for 1b.eqtlevel2.1 since it hasn't any sublevel item.
Now the problem is slightly different. How I can prevent this error when reaching objects which have not sublevel items since the same method has to be applied whitout knowing in advance the content of the pointed objects?
Thank again for your availability.
RV
|
|
|
|
|
The problem seems to be solved by putting the block:
if(eqtlevel2 !=null)
{
foreach (EqtLevel2 eqtl2 in eqtlevel2)
{
Console.WriteLine(eqtl2.EqtLevel2Name);
}
}
Console.ReadLine();
ahead of the incriminated line.
Thank you again.
|
|
|
|
|
Hahaha; yes, I was looking at the wrong line; it is not the line that you state that goes wrong, but the line after that;
EqtLevel2[] eqtlevel2 = EqtListDataBase.GetSetOfEqtLevel2(eqtlevel1[i]);
for (int j = 0; j < eqtlevel2.Length; j++) If your eqtlevel2 is null, then it is not possible to fetch its "Length" property. That would be the source of the nullreference-exception, and the stacktrace should reflect that.
Instead of having a check whether or not the method returns null, I'd prefer the method to return an empty array or list instead of null. That way you can enumerate it even if it is empty, without having to check if it is null or not.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.
Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.
We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!
Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.
But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you for your reply OriginalGriff.
I've provided additional details in the messages above.
It seems that the problem has just been solved.
I've written the following line for preventing the application for execution the incriminated line by putting the following instruction:
if(eqtlevel2 !=null)
{
foreach (EqtLevel2 eqtl2 in eqtlevel2)
{
Console.WriteLine(eqtl2.EqtLevel2Name);
}
}
Console.ReadLine();
Thank you again.
RV
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi guys, I have a question about Microsoft Report Viewer C #. So my problem is this: I have a report generated with the previous version of the report viewer, now I have updated the current version of the report viewer, but now when I modify a report this xmlns is set with the report version 2016, The problem is that it does not work! how do I make sure the xmlns link does not update?
|
|
|
|
|