|
you don't show (us) some of the more important/pertinent information we would need to be able to help you - for example :-
Member 11956589 wrote: which is filled from a SQL database using a SELECT query - seeing how you do this might help
You don't say / indicate (unless Im going blind in my dotage) WHERE your exception occurs
Finally - you're starting with a query - why don't you run a loop over the rows in ds.Tables["Animals"] and/or do some other diagnostics first, to see/prove it is being populated correctly - else your query will be worthless if you're not getting that far
[edit]
Think about something like this - before you attempt your query, to see if the data[set|table] is being populated correctly
foreach (DataRow animalsDataRow in ds.Tables["Animals"].Rows)
{
}
[/edit]
modified 22-Sep-15 20:51pm.
|
|
|
|
|
Member 11956589 wrote: and tells me that there is a Null value. Yes. Either the dataset ds is null or there's no such table as "Animals" in it.
You can use Quick Watch under Visual Studio to find out which one it is. Add a debug point and hover (move your mouse pointer) over ds and see if it's null. If it's not, then see if it has the "Animals" table using Quick Watch or the Immediate Window.
You have just been Sharapova'd.
|
|
|
|
|
It is correct because you do not initialize your Dataset
DataSet ds = new Dataset();
You did not say any word how did you filled your dataset.
You can fill it with your search results like this:
private void searchBtn_Click(object sender, EventArgs e)
{
string connectionString = "Data Source= ... your connection string";
SqlDataAdapter da = new SqlDataAdapter(
"SELECT your fields " +
"FROM Animals" +
"WHERE gender = @searchFor",
connectionString);
da.SelectCommand.Parameters.AddWithValue("@searchFor", tbSearch.Text);
da.Fill(ds);
DataTable animals = ds.Tables["Animals"];
foreach (DataRow row in animals.Rows)
{
}
}
P.S: It is a bad practice to proceed SQL-query as string by adding values with "+". Please look for "sql injections".
It would be better to use Parameters.AddWithValue() method.
|
|
|
|
|
I´ve got a question regarding inheritage with multible abstract classes. Say I´ve got these classes:
public abstract class Animal
{
public abstract void MyMethod;
}
public abstract class Mammal : Animal
{
public abstract void MyMethod;
}
public class Dog : Mammal
{
public override void MyMethod()
{...}
}
Animal and Mammal are both abstract. Now I dont want the method MyMethod() implemented in Mammal, but I want it implemented in Dog. How can I do? I know the code above doesnt compile.
|
|
|
|
|
Ahem, just found the answer by myself. I leave the method out altogether in the Mammal class.
|
|
|
|
|
Try this:
namespace YourNameSpace
{
public abstract class Animal
{
public string Name;
public abstract string MyAbstractMethod();
public virtual string MyVirtualMethod()
{
return string.Format("Animal: {0}", Name);
}
}
public class Mammal : Animal
{
public override string MyAbstractMethod()
{
return string.Format("Mammal: {0}", Name);
}
public new string MyVirtualMethod()
{
return string.Format("Mammal: {0}", Name);
}
}
public class Dog : Mammal
{
public new string MyAbstractMethod()
{
return string.Format("Dog: {0}", Name);
}
public new string MyVirtualMethod()
{
return string.Format("Dog: {0}", Name);
}
}
} Test it like this:
namespace YourNameSpace
{
private void TestAnimal()
{
Dog myDog = new Dog();
myDog.Name = "Spot";
string dname = myDog.MyAbstractMethod();
string mname = (myDog as Mammal).MyAbstractMethod();
string aname = (myDog as Animal).MyAbstractMethod();
string dname2 = myDog.MyVirtualMethod();
string mname2 = (myDog as Mammal).MyVirtualMethod();
string aname2 = (myDog as Animal).MyVirtualMethod();
}
}
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
i have Windows-Mobile program that work with weight Connecting through rs232.
its work excellent, but If the program works in a period of time -
fonts are Changing alone from big to small font
I noticed that the problem is in the first part 1 - if i mark this part1
The fonts will not change. (I have no idea why this is happening....)
my code:
//PART 1
port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recepcion);
private SerialPort port;
StringBuilder SB;
private void Recepcion(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
SB = new StringBuilder(1000);
Application.DoEvents();
System.Threading.Thread.Sleep(122);
SB.Append(port.ReadExisting());
port.DiscardInBuffer();
this.Invoke(new EventHandler(Actualizar));
}
catch { }
}
string MOMO1, MOMO2;
string[] WI;
string ALL;
private void Actualizar(object s, EventArgs e)
{
ALL = SB.ToString().Trim();
WI = ALL.Split(',');
ALL = WI[2].ToString().Trim();
MOMO1 = ALL.Replace("+", "").Replace("g", "").Replace("ST", "").Replace("GS", "").Replace("US", "");
if (MOMO1 != "")
{
MOMO2 = MOMO1;
}
lblMSG.Font = new Font("Ariel", 48, FontStyle.Bold);
lblMSG.Text = MOMO2;
Check_Weight();
GC.Collect();
}
Can anyone advise me why and how to solve it ?
|
|
|
|
|
I refer the gentleman to my reply to his question earlier today[^] - which I see he has ignored completely...
Is there any point in asking questions if you are going to ignore the answers?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I tried to do everything offered, but not helped yet
|
|
|
|
|
Clearly, you haven't tried very hard - given that over 5 hours later, the code is still exactly the same...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
This is still bad code - it looks to be exactly the same as the one you posted earlier. To add to OG's earlier comments - Application.DoEvents followed by a Thread.Sleep - this tells me you are trying to force it to wait for data and hoping that you will magically sync with the data. Oh, and you shouldn't have an empty catch statement - you have no idea what exceptions you are catching and throwing away.
|
|
|
|
|
I'm using WPF Modal Dialog for an Overlay.
Now I need to kepp this WPF-Window to be ALWAYS on top.
Setting Topmost="True" in the MainWindow.xaml isn't enough.
When an other Application (fullscreen) becomes the TOP my App hides in the background.
Now I found this tool:
Utility to make important windows remain always on top
Is there a way to include the function of the tool directly into my WPF?
Sorry for my bad english ...
|
|
|
|
|
Member 10476529 wrote: Now I need to kepp this WPF-Window to be ALWAYS on top. Bad idea[^]. If it is an overlay, then you want to set the owners' form as its parent, so it is always on top of that.
The utility will not work differently from the property. If you "need" this functionality anyway, check out SO[^] for a suggestion.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, I "need" this functionality absolutely.
It's an Overlay that has to be used for restarting when other apps hung oder havin some kind of error messages.
I tried your link but can't sign my app ?!?
I set the manifest this way:
<requestedExecutionLevel level="asInvoker" UiAccess="true" />
But then I get the Message from SharpDevelop:
Unhandled WPF exception
Exception thrown:
System.ComponentModel.Win32Exception (0x80004005): Diese Anwendung konnte nicht gestartet werden, da die Side-by-Side-Konfiguration ungültig ist. Weitere Informationen finden Sie im Anwendungsereignisprotokoll. Weitere Details können auch mit dem Befehlszeilentool sxstrace.exe angezeigt werden
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at ICSharpCode.SharpDevelop.Services.WindowsDebugger.StartWithoutDebugging(ProcessStartInfo processStartInfo)
at ICSharpCode.SharpDevelop.Project.DefaultProjectBehavior.Start(Boolean withDebugging)
at ICSharpCode.SharpDevelop.Project.ProjectBehavior.Start(Boolean withDebugging)
at ICSharpCode.SharpDevelop.Project.ProjectBehavior.Start(Boolean withDebugging)
at ICSharpCode.SharpDevelop.Project.ProjectBehavior.Start(Boolean withDebugging)
at ICSharpCode.SharpDevelop.Project.ProjectBehavior.Start(Boolean withDebugging)
at ICSharpCode.SharpDevelop.Project.AbstractProject.Start(Boolean withDebugging)
at ICSharpCode.SharpDevelop.Project.Commands.Execute.<>c__DisplayClass1.<Run>b__0(Object param0, EventArgs param1)
at ICSharpCode.SharpDevelop.Project.Commands.AbstractBuildMenuCommand.OnBuildComplete(EventArgs e)
at ICSharpCode.SharpDevelop.Project.Commands.AbstractBuildMenuCommand.CallbackMethod(BuildResults results)
at ICSharpCode.SharpDevelop.Project.BuildEngine.<ReportDone>b__3()
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
---- Recent log messages:
13:32:18.552 [1] DEBUG - ActiveWorkbenchWindowChanged to [AvalonWorkbenchWindow: WpfModalDialog*]
13:32:18.552 [1] DEBUG - NavigationService
ActiveViewContent: WpfModalDialog
Subview: TabPageText
13:32:18.552 [1] DEBUG - ActiveViewContentChanged to ICSharpCode.SharpDevelop.Project.Dialogs.ProjectOptionsView
13:32:22.153 [1] DEBUG - Activated feature 'ICSharpCode.SharpDevelop.Project.Commands.ExecuteWithoutDebugger', activation=Toolbar
13:32:22.161 [1] DEBUG - Activated feature 'ICSharpCode.SharpDevelop.Project.BuildEngine.Build'
13:32:22.162 [1] INFO - Start building WpfModalDialog
13:32:22.162 [1] DEBUG - CompilerMessageView: Combined 2 appends.
13:32:22.162 [26] DEBUG - Setting property CurrentSolutionConfigurationContents to '<SolutionConfiguration><ProjectConfiguration Project="{38951BDB-95FB-46CE-900E-0B4FF29292F9}">Release|AnyCPU</ProjectConfiguration></SolutionConfiguration>'
13:32:22.162 [26] DEBUG - Setting property SolutionDir to 'C:\temp\SharpDevelop\wpfmodaldialog\'
13:32:22.162 [26] DEBUG - Setting property SolutionExt to '.sln'
13:32:22.162 [26] DEBUG - Setting property SolutionFileName to 'WpfModalDialog.sln'
13:32:22.162 [26] DEBUG - Setting property SolutionName to 'WpfModalDialog'
13:32:22.162 [26] DEBUG - Setting property SolutionPath to 'C:\temp\SharpDevelop\wpfmodaldialog\WpfModalDialog.sln'
13:32:22.263 [17] DEBUG - Received command ReportEvent
13:32:22.309 [17] DEBUG - Received command ReportEvent
13:32:22.322 [17] DEBUG - Received command ReportEvent
13:32:22.581 [17] DEBUG - Received command ReportEvent
13:32:22.873 [17] DEBUG - Received command ReportEvent
13:32:22.889 [17] DEBUG - Received command ReportEvent
13:32:22.892 [17] DEBUG - Received command ReportEvent
13:32:22.893 [17] DEBUG - Received command BuildDone
13:32:22.894 [17] INFO - Finished building WpfModalDialog, success=True
13:32:22.894 [17] INFO - Start building WpfModalDialog
13:32:22.894 [14] INFO - Finished building WpfModalDialog, success=True
13:32:22.895 [1] INFO - Debugger Command: Start (withDebugger=False)
---- Post-error application state information:
Installed 3rd party AddIns:
Workbench.ActiveContent: ICSharpCode.SharpDevelop.Project.Dialogs.ProjectOptionsView
ProjectService.OpenSolution: [Solution: FileName=C:\temp\SharpDevelop\wpfmodaldialog\WpfModalDialog.sln, HasProjects=True, ReadOnly=False]
ProjectService.CurrentProject: [CSharpProject: WpfModalDialog]
And when I start my exe it gaves me an Side-By-Side Error:
Fehler beim Generieren des Aktivierungskontextes für "C:\temp\SharpDevelop\wpfmodaldialog\WpfModalDialog\bin\Release\Test.exe".
Fehler in Manifest- oder Richtliniendatei "C:\temp\SharpDevelop\wpfmodaldialog\WpfModalDialog\bin\Release\Test.exe" in Zeile 18.
Das "UiAccess"-Attribut ist in diesem Kontext nicht zulässig im requestedExecutionLevel-Element.
modified 22-Sep-15 8:08am.
|
|
|
|
|
Did you sign it the executable before running?
As SO notes, it is not some trivial task; the UiAccess element is described on MSDN, here[^]. Will also require the other steps before it runs.
If I read the post right, you could skip a step if you modify the registry.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
modified 22-Sep-15 8:24am.
|
|
|
|
|
Tried UiAccess-methode seperate times => doesn't work
So I tried this:
Keep my application on top[^]
It is now implemented in the App.xaml.cs this way:
namespace Technewlogic.Samples.WpfModalDialog
{
public partial class App : Application
{
public bool TopMost { get; private set; }
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 SetForegroundWindow(int hWnd);
private void triMain_Click(object sender, System.EventArgs e)
{
int hWnd = FindWindow(null, "Test");
SetForegroundWindow(hWnd);
TopMost = true;
Show();
}
private void Show()
{
throw new NotImplementedException();
}
...
Is there a method to put a timer in this so the application always becomes TopMost (about every 10 seconds)?
|
|
|
|
|
Benjamin.Buhr wrote: Is there a method to put a timer in this so the application always becomes TopMost (about every 10 seconds)? IIRC, then WPF has its own timer. Here[^] it is.
Execute your "triMain_Click" code whenever the timer fires.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I tried it this way:
...
public partial class App : Application
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
SystemSounds.Beep.Play();
InitializeComponent();
}
public bool TopMost { get; private set; }
...
I included a Beep to check if the timer is fired - but no sound appears???
Do I have to include this in the MainWindow?
|
|
|
|
|
You've declared and created the timer in a method; I'd assume it gets disposed as soon as it exits the method. Try something like below;
public partial class App : Application
{
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
It's a little bit confusing now ... I found one mistake :-|
The App.xaml is defined this way:
<Application x:Class="Technewlogic.Samples.WpfModalDialog.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
So I had to correct the fiering of the timer in the App.xaml.cs this way:
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using System.Windows;
namespace Technewlogic.Samples.WpfModalDialog
{
public partial class App : Application
{
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
TopMost = true;
this.TopMost = true;
InitializeComponent();
}
public bool TopMost { get; set; }
[SuppressUnmanagedCodeSecurity]
public class SafeNativeMethods
{
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 SetForegroundWindow(int hWnd);
}
private void triMain_Click(object sender, System.EventArgs e)
{
int hWnd = SafeNativeMethods.FindWindow(null, "Test");
SafeNativeMethods.SetForegroundWindow(hWnd);
TopMost = true;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
bool createdNew;
Mutex mutex = new Mutex(true, this.GetType().GUID.ToString(), out createdNew);
if (createdNew)
{
Window window = new MainWindow();
window.Closed += (sender2, args) => mutex.Close(); ;
window.Show();
}
else
{
mutex.Close();
Application.Current.Shutdown();
}
}
}
}
I've tested the timer with a MessageBox => worked
But how is it possible to renew the TopMost for the MainWindow (Mainwindow.xaml.cs) or for the App?
Should I use the TopMost-Function better in the MainWindow-Element?
And I have a nother question:
This makes a CA1401:
public class SafeNativeMethods
{
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 SetForegroundWindow(int hWnd);
}
How can I correct this?
|
|
|
|
|
My corrections didn't work ...
So I will now try this way:
Now my MainWindow.xaml.cs is coded this way:
using System;
using System.Diagnostics;
using System.Windows;
using System.Runtime.InteropServices;
namespace Technewlogic.Samples.WpfModalDialog
{
public partial class MainWindow : Window
{
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
const int swRestore = 9;
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
var me = Process.GetCurrentProcess();
var arrProcesses = Process.GetProcessesByName(me.ProcessName);
if (arrProcesses.Length > 1)
{
for (var i = 0; i < arrProcesses.Length; i++)
{
if (arrProcesses[i].Id != me.Id && arrProcesses[i].SessionId == me.SessionId)
{
IntPtr hWnd = arrProcesses[i].MainWindowHandle;
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, swRestore);
}
SetForegroundWindow(hWnd);
break;
}
}
}
}
public MainWindow()
{
Window w = new Window();
w.Top = -100;
w.Left = -100;
w.Width = 1;
w.Height = 1;
w.WindowStyle = WindowStyle.ToolWindow;
w.Show();
this.Owner = w;
w.Hide();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
dispatcherTimer.Start();
InitializeComponent();
ModalDialog.SetParent(ModalDialogParent);
}
private void ShowModalDialog_Click(object sender, RoutedEventArgs e)
{
var res = ModalDialog.ShowHandlerDialog(MessageTextBox.Text);
var resultMessagePrefix = "Result: ";
if (res)
ResultText.Text = resultMessagePrefix + "OK";
else
ResultText.Text = resultMessagePrefix + "Break";
}
}
}
|
|
|
|
|
What does "didn't work" mean? Did it throw an exception? Failed silently? Did the timer fail, or getting to topmost?
The previous link from SO is the only real supported scenario; I'd recommend focussing on that.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Sorry for giving not enough informations about the fail
=> getting topmost failed
There is no exception and the timer is working without errors...
|
|
|
|
|
If you open a normal Window, it shows in front of your window?
Does the same happen without timer?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, it is shown in front of a normal window.
I think the other problem ist that I'm using UserControl in WPF.
Because of this the handle will always be 0 ...
Is there a way to set/create/place a handle in my WPF including UserControl?
The Console-Outputs I created always say this:
********************** Timer_Tick-Anfang **********************
Setze ShowWindowAsync für den System.Diagnostics.Process (Test)-Handle 0 mit dem SW_RESTORE-Parameter 9
Setze ForegroundWindow für System.Diagnostics.Process (Test) mit dem Handle 0
Setze TOPMOST für Prozess System.Diagnostics.Process (Test) mit dem Handle 0
Setze TOP für Prozess System.Diagnostics.Process (Test) mit dem Handle 0
hWnd: 0
********************** Timer_Tick-Ende ************************
I am loading the UserControl in the main window this way:
InitializeComponent();
ModalDialog.SetParent(ModalDialogParent);
|
|
|
|