TextBox In DataContext

Hi! How possible to save data from TextBox with DataContext to sql Server?

In my XAML  (WinPage.XAML) i have the TextBox namely TxtBox1, TxtBox2, TxtBox3 and Button name button1. I Have a Datacontext name  WinOrganizationContext. So, now in XAML.cs I put some code in my button1 as follows:

        private void button1_Click(object sender, RoutedEventArgs e)
            WinOrganizationContext _iContext = new WinOrganizationContext();
            _iContext.ID = TxtBox1.Text;
            _iContext.xName = TxtBox2.Text;
            _iContext.fName = TxtBox3.Text;

            MessageBox.Show("New files has been saved!")

_iContext is geeting error! And I do not know if i could insert new files on this. Is this possible?
Whing Dela CruzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gautham JanardhanCommented:
what is the error you are getting ?

ideally if the data context of ur view is WinOrganizationContext  then you can do this in your xaml to directly bind text to property inside WinOrganizationContext  

<TextBox Text="{Binding ID }"  x:Name="TxtBox1"/>
<TextBox Text="{Binding xName }"  x:Name="TxtBox2"/>
<TextBox Text="{Binding fName }"  x:Name="TxtBox3"/>

Open in new window

Whing Dela CruzAuthor Commented:
Hi! How to save it to my database?
      I'm trying to follow as what instructed above so heres my xaml
      <TextBox Text="{Binding BranchName }"  x:Name="TxtBox1"/>
      <Button Height="40" Content="Save" Name="Save" Click="Save_Click"/>

I have also created a Class name AppState.cs as follows;

    public static WinOrganizationContext WinOroganizationContext { get;  set;}
    public static SalesRecord _record { get; set; }

In my xaml.cs are as follows;

   private void Save_Click(object sender,RoutedEventsArgs e)
   AppState._record.BranchName = TxtBox1.Text;
   AppState._record.Cash = 0;
   MessageBox("New files has been added");

When I tried to execute and Click save an error says;
  Object reference not set to an object..

Help Please...
Gautham JanardhanCommented:
where are you initialising the properties inside AppState ? and i dont see any code setting the data context of the view. unless you set the data context of the view, there is not point in giving binding in xaml
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Whing Dela CruzAuthor Commented:
Hi! Do I need to use dataGrid? How to do that? Can you give me some example?

I'm trying now... but got the same error...
Gautham JanardhanCommented:
datagrid is used if the user will update multiple records at a time , is that the case here ?

see sample below

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" SizeToContent="WidthAndHeight">
            <ColumnDefinition />
            <ColumnDefinition Width="200"/>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        <TextBlock Margin="4" Grid.Column="0" Text="Id" />
        <TextBox Margin="4" Grid.Column="1" Grid.Row="0" Text="{Binding ID}"  x:Name="TxtBox1"/>
        <TextBlock Margin="4" Grid.Column="0" Grid.Row="1" Text="Name" />
        <TextBox Margin="4" Grid.Column="1" Grid.Row="1" Text="{Binding xName}"  x:Name="TxtBox2"/>
        <TextBlock Margin="4" Grid.Column="0" Grid.Row="2" Text="F Name" />
        <TextBox Margin="4" Grid.Column="1" Grid.Row="2" Text="{Binding fName}"  x:Name="TxtBox3"/>
        <Button Content="Save" Click="Button_Click" Grid.Row="4" Grid.Column="1" Width="100" Margin="5" HorizontalAlignment="Right" />

Open in new window

    public partial class MainWindow : Window
        public MainWindow()
            this.DataContext = AppState.WinOrganizationContex;

        private void Button_Click(object sender, RoutedEventArgs e)
            MessageBox.Show(string.Format("Id - {0} ,x name -{1} ,f name -{2}" , AppState.WinOrganizationContex.ID,

Open in new window

    public static class AppState
        public static WinOrganizationContex WinOrganizationContex { get; set; }
        static AppState()
            WinOrganizationContex = new WinOrganizationContex();

    public class  WinOrganizationContex
        public string ID { get; set; }
        public string xName { get; set; }
        public string fName { get; set; }

Open in new window

Whing Dela CruzAuthor Commented:
Hi! I tried to follow all steps stated above but when execute save command no data has been saved. What are the things do I need to this Command?

        private void Button_Click(object sender, RoutedEventArgs e)
                AppState.WinOrganizationContex.xName = TxtBox2.Text;
                AppState.WinOrganizationContex.fName = TxtBox3.Text;
                MessageBox.Show("New file has been added");
is there any maneuver needed to this?
Gautham JanardhanCommented:
the code i gave will only update to the object, it wont "save" any data to stores (db,xml etc)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Whing Dela CruzAuthor Commented:
Thanks! Can you provide me some xample that can Save data using this kind of scenario? When I save new data i use Silverlight Child window... but I need some alternatives that can save data using directly textBox as shown above... Thanks a lot!
Gautham JanardhanCommented:
Whing Dela CruzAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.