Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What concepts should I study to accomplish this?

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.

like image 290
JRB Avatar asked Mar 16 '11 22:03

JRB


1 Answers

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.

like image 91
Brent Stewart Avatar answered Oct 11 '22 17:10

Brent Stewart