oledbexception is thrown for input that's accepted by access - c#

I'm building a small phonebook application and I'm storing phones in an access database in table called phones whose fields are : FullName,Job,Phone1,Phone2,Notes and SN primary key.
I've set fullname and phone1 to required and set validation rule for fullname to Len([FullName])>3 and validation rule for both phone1 and phone2 to Like "[0-9]*", I also set validation messages for the validation rules.
In a wpf application I've added the access database using visual studio to generate the dataset and tableadapter code, this is MainWindow.xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="253" Width="685">
<Grid Height="206" Width="658">
<Label Content="FullName" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Label1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="78,16,0,0" Name="FullName" VerticalAlignment="Top" Width="185" />
<Label Content="Job" Height="28" HorizontalAlignment="Left" Margin="29,46,0,0" Name="Label2" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="78,46,0,0" Name="Job" VerticalAlignment="Top" Width="185" />
<Label Content="Phone1" Height="28" HorizontalAlignment="Left" Margin="22,75,0,0" Name="Label3" VerticalAlignment="Top" />
<Label Content="Phone2" Height="28" HorizontalAlignment="Left" Margin="269,16,0,0" Name="Label4" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="78,75,0,0" Name="Phone1" VerticalAlignment="Top" Width="110" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="325,21,0,0" Name="Phone2" VerticalAlignment="Top" Width="110" />
<Label Content="Notes" Height="28" HorizontalAlignment="Left" Margin="274,56,0,0" Name="Label5" VerticalAlignment="Top" />
<TextBox Height="95" HorizontalAlignment="Left" Margin="325,60,0,0" Name="Notes" VerticalAlignment="Top" Width="328" AcceptsReturn="True" AcceptsTab="True" />
<Button Content="Save" Height="37" HorizontalAlignment="Left" Margin="274,161,0,0" Name="Button2" VerticalAlignment="Top" Width="135" />
</Grid>
this is save click handler :
private button2_Click(object sender , RoutedEventArgs e)
{
try
{
PhonesDataSetPhonesTableAdapter.Insert(FullName.Text, Job.Text, Phone1.Text, Phone2.Text, Notes.Text);
} catch(OleDbException ex) {
MessageBox.Show(ex.Message);
}
}
the PhonesDataSetPhonesTableAdapter is defined like this :
Global.projectName.PhonesDataSetTableAdapters.PhonesTableAdapter PhonesDataSetPhonesTableAdapter = new Global.projectName.PhonesDataSetTableAdapters.PhonesTableAdapter();
When I launch application and put John as fullname and programmer as job and 2137976 as phone1 number an OleDbException is thrown with the validation message for phone1 which is
your phone number contains letters, it should only contain numbers
but it doesn't contain any letter and when I try the same input through access it accepts it, what's going on here ? and how do I make it work.

The OleDb driver requires a different wildcard character in its Like statement. Instead of * it now wants a % (which is ANSI-92).
If I change in MS Access the validation rule to Like "[0-9]%" I can insert rows with a phonenumber "123" by calling insert on that tableadapter.
The downside of this is that in Access you can no longer insert values as Access now expects the literal character % after a single digit.
If you want both your application and access to work, using the Odbc driver would work.
Here is some background on the issue: Microsoft Jet wildcards: asterisk or percentage sign?

Related

Refresh Button UWP

I want to add a refresh button to my app so that I don't always have to quit the app to clear the data.
I have tried a RefreshRequested, but I can't make it work.
C# code:
private void RefreshButtonClick(object sender, RoutedEventArgs e)
{
RefreshContainer.requestRefresh();
}
<RefreshContainer>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="910" Height="383" Margin="0,0,0,0">
<RelativePanel
HorizontalAlignment="Center" BorderBrush="DarkOliveGreen" BorderThickness="8" Background="FloralWhite" Height="356" VerticalAlignment="Center" Width="871" Margin="0,0,0,0" Visibility="Visible" RequestedTheme="Default">
<AppBarButton x:Name="RefreshButton" Click="RefreshButtonClick"
Icon="Refresh" Label="Refresh" HorizontalAlignment="Left" Margin="155,178,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name="timerLabel" HorizontalAlignment="Left" Text="00:00:00" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="72" FontWeight="Normal" TextAlignment="Center" Width="328" Margin="272,90,0,0"/>
<Button Name="startButton" Background="MediumSeaGreen" Foreground="White" Content="Start" HorizontalAlignment="Left" Click="Button_Click_Start" VerticalAlignment="Top" Margin="311,219,0,0"/>
<Button Name="stopButton" Background="MediumSeaGreen" Foreground="White" Content="Pause" HorizontalAlignment="Left" Click="Button_Click_Pause" VerticalAlignment="Top" Margin="398,219,0,0" RenderTransformOrigin="1.373,0.57"/>
<Button Name="resetButton" Background="MediumSeaGreen" Foreground="White" Content="Reset" HorizontalAlignment="Left" Click="Button_Click_Reset" VerticalAlignment="Top" Margin="498,219,0,0" RenderTransformOrigin="1.373,0.57"/>
<Button Name="restButton" Content="Parametres" HorizontalAlignment="Left" Click="Button_Click_Rest" VerticalAlignment="Top" Margin="698,192,0,0" RenderTransformOrigin="1.373,0.57"/>
<TextBlock Name="Round" Text="Nombre Round" HorizontalAlignment="Left" Height="20" Margin="382,70,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="140" TextDecorations="Underline"/>
</RelativePanel>
</Grid>
</RefreshContainer>
I expect the app data to refresh
Refresh Button UWP
I tested your code, the problem is you have not set a scroll-able control as RefreshContainer's content. Base on #Stuart Smith provided document, you could realize refresh feature, before that you need place the above Grid content under the ScrollViewer.
Xaml
<RefreshContainer Name="MyRefreshContainer" RefreshRequested="MyRefreshContainer_RefreshRequested">
<ScrollViewer>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="910" Height="383" Margin="0,0,0,0">
<RelativePanel
HorizontalAlignment="Center" BorderBrush="DarkOliveGreen" BorderThickness="8" Background="FloralWhite" Height="356" VerticalAlignment="Center" Width="871" Margin="0,0,0,0" Visibility="Visible" RequestedTheme="Default">
<AppBarButton x:Name="RefreshButton" Click="RefreshButtonClick"
Icon="Refresh" Label="Refresh" HorizontalAlignment="Left" Margin="155,178,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name="timerLabel" HorizontalAlignment="Left" Text="00:00:00" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="72" FontWeight="Normal" TextAlignment="Center" Width="328" Margin="272,90,0,0"/>
<Button Name="startButton" Background="MediumSeaGreen" Foreground="White" Content="Start" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="311,219,0,0"/>
<Button Name="stopButton" Background="MediumSeaGreen" Foreground="White" Content="Pause" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="398,219,0,0" RenderTransformOrigin="1.373,0.57"/>
<Button Name="resetButton" Background="MediumSeaGreen" Foreground="White" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="498,219,0,0" RenderTransformOrigin="1.373,0.57"/>
<Button Name="restButton" Content="Parametres" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="698,192,0,0" RenderTransformOrigin="1.373,0.57"/>
<TextBlock Name="Round" Text="Nombre Round" HorizontalAlignment="Left" Height="20" Margin="382,70,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="140" TextDecorations="Underline"/>
</RelativePanel>
</Grid>
</ScrollViewer>
</RefreshContainer>
Code behind
private void RefreshButtonClick(object sender, RoutedEventArgs e)
{
MyRefreshContainer.RequestRefresh();
}
private async void MyRefreshContainer_RefreshRequested(RefreshContainer sender, RefreshRequestedEventArgs args)
{
using (var RefreshCompletionDeferral = args.GetDeferral())
{
// Do some async operation to refresh the content
RefreshCompletionDeferral.Complete();
RefreshCompletionDeferral.Dispose();
}
}
Tomas, I see you've abandoned the refresh button for now, but I wanted to follow up in case you revisit this later or if others run into this issue in the future.
In order for the data to refresh, you'd need to code this in.
“To get fresh content when needed, handle the RefreshRequested event. In the event handler, you'll need code specific to your app to get the fresh content.” https://learn.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/pull-to-refresh#handle-a-refresh-request. There's a sample in the doc there too.
You'd typically use data binding for this and the RefreshRequested event would need to edit the bound object.
using (var RefreshCompletionDeferral = args.GetDeferral())
{
await FetchAndInsertItemsAsync(3);
}
In the sample, you'll notice that FetchAndInsertItemsAsync is inserting new items to the backing collection.
There's more info on data binding here: https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-quickstart

Property error in Windows phone development in C# and XAML

This is a Water Hackathon, 2014 Project.
I am a newbie in windows phone. I was developing an app for a project. part of the app is that user will write his number and some text and the number and text will be emailed to a particular person.
I used drag n drop UI and wrote the code of email send in Visual C#. Here is the error:
Failed to assign to property 'System.Windows.UIElement.AllowDrop'.[Line: 53 Position: 76]
at
System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at
PhoneApp2.MainPage.InitializeComponent()
at PhoneApp2.MainPage..ctor()
part of the C# code:
private void emailSend(object sender, RoutedEventArgs e)
{
// Create a new instance of the class EmailComposeTask with which you can send email
var emailcomposer = new EmailComposeTask
{
// I enter the recipients to send the email using the To property of the class
EmailComposeTask
To = string.Concat("mailto:","ibtehaz.shawon#gmail.com"),
// Set the title of the property by Subject
Subject = "number",
// Enhanced the value of the Body property EmailComposeTask class, this is the
content that will display the recipient
Body = bigText.Text,
};
// Start the email application on your device to send the Email
emailcomposer.Show();
}
part of the XAML file:
<TextBlock Text="Wheres the problem" Style="{StaticResource PhoneTextNormalStyle}"
FontSize="36" FontFamily="Andalus"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,0,14,0"
RenderTransformOrigin="0.463,0.232">
<TextBox Name="number" HorizontalAlignment="Left" Height="88" Margin="0,98,0,0"
TextWrapping="Wrap" Text="Please enter your phone number" VerticalAlignment="Top"
Width="456"/>
<TextBlock Name="bigText" HorizontalAlignment="Left" AllowDrop="True" TextWrapping="Wrap"
Text="Users will write their problem in here." VerticalAlignment="Top"
Margin="0,231,0,0" Height="414" Width="456" Foreground="White"/>
</Grid>
<Button Name="sendButton" Content="Send your problem" HorizontalAlignment="Stretch"
VerticalAlignment="Top" Margin="10,658,0,0" Grid.Row="1" BorderBrush="#FF996D6D"
Background="#FF081359"/>
Thank you in advance.
You need to replace TextBlock. TextBlock is only used for displaying text, so you can't edit it (eg, drop text onto it). TextBox allows input and will let you drop text into it.
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,0,14,0"
RenderTransformOrigin="0.463,0.232">
<TextBox Name="number" HorizontalAlignment="Left" Height="88" Margin="0,98,0,0"
TextWrapping="Wrap" Text="Please enter your phone number" VerticalAlignment="Top"
Width="456"/>
<TextBox Name="bigText" HorizontalAlignment="Left" AllowDrop="True" TextWrapping="Wrap"
Text="Users will write their problem in here." VerticalAlignment="Top"
Margin="0,231,0,0" Height="414" Width="456" Foreground="White"/>
</Grid>
<Button Name="sendButton" Content="Send your problem" HorizontalAlignment="Stretch"
VerticalAlignment="Top" Margin="10,658,0,0" Grid.Row="1" BorderBrush="#FF996D6D"
Background="#FF081359"/>

Can't seem to set Combobox.Background

I'm developing a Turing Machine simulator for a class on theory, and I'm trying to change the background color of the input area based upon whether the machine would accept the language (basically, one color over the other depending on if it's valid input).
Since I want to provide a couple example inputs, it needs to be a ComboBox. Since the professor needs to test his own inputs, it must be editable as well. So, here we are.
I've tried setting the ComboBox.Background property both programmatically and using XAML (via the Property editor), and neither work. I have no problem setting ComboBox.Foreground, however.
Here is my XAML:
<Window x:Class="Turing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Turing Machine Emulator" Height="400" Width="600" Loaded="onload" MinHeight="500" MinWidth="600">
<Grid>
<ComboBox x:Name="drpProblem" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="changeproblem"/>
<Label x:Name="lblDescription" Content="Language Description" Margin="135,7,90,0" VerticalAlignment="Top"/>
<Grid Margin="10,0,10,35" Height="24" VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="10*"/>
</Grid.ColumnDefinitions>
<Label x:Name="lblLeft" Content="left" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="0" Margin="0,0,10,0" FontFamily="Consolas"/>
<Label x:Name="lblRight" Content="right" Grid.Column="2" VerticalContentAlignment="Center" Padding="0" Margin="10,0,0,0" FontFamily="Consolas"/>
<Label x:Name="lblCenter" Content="cur" Grid.Column="1" Height="24" Padding="0" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Background="#FFC5FFA4" FontFamily="Consolas" FontSize="16"/>
</Grid>
<Button x:Name="btnIterate" Content="Iterate" Margin="10,0,0,64" Height="20" VerticalAlignment="Bottom" Click="btnIterate_Click" HorizontalAlignment="Left" Width="236"/>
<!-- This one right here -->
<ComboBox x:Name="txtInput" Height="23" Margin="10,0,10,89" Text="Input String" VerticalAlignment="Bottom" FontFamily="Consolas" VerticalContentAlignment="Center" TextBoxBase.TextChanged="cboGetInput" BorderBrush="{x:Null}" Foreground="Black" Background="#FF874343" IsEditable="True" />
<TextBox x:Name="txtMs" Height="20" Margin="251,0,172,64" TextWrapping="Wrap" Text="wait (seconds)" VerticalAlignment="Bottom"/>
<Button x:Name="btnAutoRun" Content="AutoRun" Margin="0,0,10,64" Click="btnAutoRun_Click" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="157"/>
<TextBox x:Name="txtTM" Margin="10,38,10,142" TextWrapping="Wrap" Text="Language" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" FontFamily="Consolas" FontSize="14"/>
<Button x:Name="btnLoadLang" Content="Load" Margin="10,0,10,117" Height="20" VerticalAlignment="Bottom" Click="changeproblem"/>
<StatusBar Height="30" VerticalAlignment="Bottom">
<TextBlock x:Name="stTXTName" Text="StateName"/>
<Separator/>
<TextBlock x:Name="stTXTDescription" Text="StateDescription"/>
<Separator/>
<TextBlock x:Name="stTXTTransition" Text="NextTransition"/>
<Separator/>
<TextBlock x:Name="stTXTNext" Text="NextState"/>
</StatusBar>
</Grid>
</Window>
and here is the code I'm using to try to change the colors around:
if (TM.AcceptsString(txtInput.Text))
{
txtInput.Background = Brushes.LightGreen;
txtInput.Foreground = Brushes.LightGreen;
}
else
{
txtInput.Background = Brushes.Pink;
txtInput.Foreground = Brushes.Pink;
}
The foreground changes as expected, but the background color never changes from the default White. Am I doing something wrong? Is there some component control within ComboBox that I need to be setting properties for, as I did with TextBoxBase.TextChanged?
Set the FlatStyle attribute of the ComboBox to FlatStyle.Flat. This resolved a similar issue I experienced when the Win 7 Aero theme is turned on: The backcolor of the ComboBox does not show up in the default FlatStyle setting FlatStyle.Standard.
I had a similar issue and was able to resolve it by auto-generating the template for the ComboBox in Visual Studio 2015 (Right Click ComboBox in Design Window -> Edit Template -> Edit a Copy)

adding controls to grid using xml file

this code converts any controls or object to xml
if I saved this
the saved xml would be looks like:
<Grid Name="g" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Image Source="pack://application:,,,/WpfApplication1;component/Images/Picture3.jpg" Stretch="Fill" Name="image1" Width="200" Height="150" Margin="96,36,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Button Name="button1" Width="75" Height="23" Margin="352,71,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">Button</Button>
<TextBox Name="textBox1" Width="120" Height="23" Margin="62,198,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" xml:space="preserve" />
<Label Name="label1" Height="28" Margin="270,213,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">Label</Label>
</Grid>
I used this code to save this:
System.IO.StreamWriter s = new System.IO.StreamWriter(#"d:\grid.xml");
s.WriteLine(System.Windows.Markup.XamlWriter.Save(g));
s.Close();
g is the name of the grid which contains all controls
my question:
can I reverse this, read this xml file and represent it on a window
Just use the XAML Reader Class.
var somecontrol = XamlReader.Load(#"<ItemsPanelTemplate
xmlns=""http://schemas.microsoft.com/client/2007""
xmlns:toolkit=""http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"">
<toolkit:WrapPanel /></ItemsPanelTemplate>");

How to set a datatable to a WPF datagrid C#?

I'm new to c# so go easy.
Basically I build a data table ( and I have verified it's not empty) but it doesn't seem to get displayed in my wpf datagrid... I used this example here and applied it:link text
My datagrid remains blank.
Does that example look fine to you guys?
here is my code:
XACML:
<Window x:Class="WpfApplication1.TickerSearch"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TickerSearch" Height="468" Width="907">
<Grid Background="#E6000000" Name="_grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="244*" />
<ColumnDefinition Width="37*" />
</Grid.ColumnDefinitions>
<TextBox Height="34" HorizontalAlignment="Left" Margin="26,15,0,0" Name="txtSearchTker" VerticalAlignment="Top" Width="225" Grid.ColumnSpan="2" />
<Button Content="Search" Height="32" HorizontalAlignment="Left" Margin="76,160,0,0" Name="btnSearch" VerticalAlignment="Top" Width="124" Click="btnSearch_Click" />
<ListBox Height="114" HorizontalAlignment="Left" Margin="26,224,0,0" Name="lstResults" VerticalAlignment="Top" Width="225" Visibility="Hidden" Grid.ColumnSpan="2" Background="#FFFFB000" />
<Button Content="Select" Height="32" HorizontalAlignment="Left" Margin="76,366,0,0" Name="btnSelect" VerticalAlignment="Top" Width="124" Visibility="Hidden" />
<Label Content="Start Date: " Height="25" HorizontalAlignment="Left" Margin="22,65,0,0" Name="label1" VerticalAlignment="Top" Width="71" Background="#00FFB000" FontFamily="Georgia" Foreground="#FFFFB000" />
<Label Content="End Date:" Height="27" HorizontalAlignment="Left" Margin="22,93,0,0" Name="label2" VerticalAlignment="Top" Width="71" Background="#00FFB000" FontFamily="Georgia" Foreground="#FFFFB000" />
<DatePicker Height="22" HorizontalAlignment="Left" Margin="93,65,0,0" Name="dateFrom" VerticalAlignment="Top" Width="138" />
<DatePicker Height="22" HorizontalAlignment="Left" Margin="93,94,0,0" Name="toDate" VerticalAlignment="Top" Width="138" />
<Label Content="Just Today" Height="27" HorizontalAlignment="Left" Margin="22,122,0,0" Name="label3" VerticalAlignment="Top" Width="71" Background="#00FFB000" FontFamily="Georgia" Foreground="#FFFFB000" />
<CheckBox Content="CheckBox" Height="17" HorizontalAlignment="Left" Margin="93,127,0,0" Name="chkBoxToday" VerticalAlignment="Top" Width="15" />
<DataGrid AutoGenerateColumns="False" Height="315" HorizontalAlignment="Left" Margin="285,68,0,0" x:Name="_dataGrid" VerticalAlignment="Top" Width="468" ItemsSource="{Binding Path=.}" Background="#C6F7F700"></DataGrid>
</Grid>
c#:
_ds = new DataSet();
DataTable table = yahooFinance.lookupSymbol();
_ds.Tables.Add(table);
_grid.DataContext = _ds.Tables[0];
Change it to ItemsSource="{Binding}".
Also, set AutoGenerateColumns to true. (Or explicitly specify columns)
Without seeing any code, I'd suggest the two most important points are:
setting the DataContext with myGrid.DataContext = myDS.Tables[0];
having your XAML DataGrid include the ItemsSource property:
<dg:DataGrid ItemsSource="{Binding Path=.}"

Categories