Solved

TextBox In DataContext

Posted on 2012-03-18
10
628 Views
Last Modified: 2012-03-24
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?
0
Comment
Question by:Whing Dela Cruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37736434
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

0
 

Author Comment

by:Whing Dela Cruz
ID: 37736888
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...
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37736921
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
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Whing Dela Cruz
ID: 37737068
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...
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37737114
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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="200"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <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" />
    </Grid>
</Window>

Open in new window


    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            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,
                AppState.WinOrganizationContex.xName,AppState.WinOrganizationContex.fName));
        }
    }

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

0
 

Author Comment

by:Whing Dela Cruz
ID: 37737638
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?
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 37737881
the code i gave will only update to the object, it wont "save" any data to stores (db,xml etc)
0
 

Author Comment

by:Whing Dela Cruz
ID: 37737967
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!
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37738037
0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 37760957
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question