I am such a newbie to c# I have to ask questions before I can get started!
What I want to do is enter a numeric number into a text box, send it to an attached sql compact database, check to see if the number is in the table, if true return the data to the form. If false, I want to run some code that will get the information and update the table, add to table, send to the form. Other then creating sql tables via C#, Could someone help me prototype this concept so to speak so I can start reading up on the concepts so that I can start building this portion of my project? Thanks.
There are many different ways to do what you are describing. A quick and easy way to handle this scenario would be to use WPF for the user interface and LINQ to SQL for the database access. There are tons of tutorials on both technologies, but here are the basic steps:
Step 1: Create a new WPF project in Visual Studio
Step 2: Add a LINQ to SQL class and map it to your Database
Step 3: Edit the MainWindow.xaml and add the input textbox, check button, and results textbox
Sample code for MainWindow.xaml (note this is quick and dirty):
<Window x:Class="WPFPlayground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Horizontal" Height="30">
<TextBox Name="InputTextBox" Width="50"/>
<Button Name="CheckButton" Content="Check DB" Click="CheckButton_Click"/>
<TextBox Name="ResultsTextBox" Width="100"/>
</StackPanel>
</Grid>
</Window>
Step 4: Edit the code behind of MainWindow.xaml.cs to handle the button click event
Sample code for Click event in MainWindow.xaml.cs (again quick and dirty)
private void CheckButton_Click(object sender, RoutedEventArgs e)
{
// Get instance of my LINQ to SQL datacontext
var db = new MyDbDataContext();
// Try to get the record that matches the text in the InputTextBox
var data = db.TableName.FirstOrDefault(r => r.Id == InputTextBox.Text);
// Was able to find a matching record so show results data
if (data != null)
{
ResultsTextBox.Text = data.EventDesc;
}
else
{
// do what ever you need to do when there is no match
}
}
Step 5: Learn some best practices and do not use this sample code :)
Have fun.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With