-1

I have form on which there is browse button , when i clicked it, dialogue box open and i selected a file now the path or file name of that file is to be show on textbox i have written code but file path is not display in text box, i have try to resolve but fail, kindly get me out of this, and there is one thing more, i want to upload it in specfic folder and then download it on my system , please tell me about this also

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.IO;
//using System.Drawing;
using System.ComponentModel;



namespace RIS_2
{
    /// <summary>
    /// Interaction logic for crud.xaml
    /// </summary>
    public partial class crud : Window
    {
        public crud()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
        }

        private void browse_btn_Click(object sender, RoutedEventArgs e)
        {
            Stream checkStream = null;
         //   OpenFileDialog op1 = new OpenFileDialog();
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Multiselect = false;
            dlg.Filter = "All Image Files | *.*";
            //dlg.Filter = "(*.pfw)|*.pfw|All files (*.*)|*.*";
           Nullable<bool> result = dlg.ShowDialog();
            // if ((bool)dlg.ShowDialog())
            if(result==true)
            {
                try
                {
                    if ((checkStream = dlg.OpenFile()) != null)
                    { 
                    //   MyImage.Source = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
                        //listBox1.Items.Add(openFileDialog.FileName);
                        string filename = dlg.FileName;
                        tb_file.Text = filename;
                       // tb_file.AppendText(dlg.FileName.ToString());
                        MessageBox.Show("Successfully done", filename);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }

            }
             else
             {

                 MessageBox.Show("Problem occured, try again later");

             }

        }


    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user3667841
  • 53
  • 2
  • 5
  • 1
    *i want to upload it in specfic folder and then download it on my system*... this is a terrible description of your requirements. After displaying the selected file path, what exactly do you want to do? – Sheridan Jun 03 '14 at 07:59
  • First i want to upload it, after successfully uploading, another user would be able to download it or view it via another form which refer to this file – user3667841 Jun 03 '14 at 08:16
  • But upload to and download from where??? The moon? We have no idea... please share your requirements with us if you *really* do want help. – Sheridan Jun 03 '14 at 08:24
  • actually i want to store file in Database (pdf, image,doc etc)and then other user can download it from database – user3667841 Jun 11 '14 at 05:47

1 Answers1

1

Frankly, to me the code looks okay, so it's strange that it doesn't work. Having said that, I'd suggest that you take a different approach. You are using WPF, which is very good at data binding. If you in the code behind define a property

private string _fileName;
public string FileName
{
 get{return _fileName;}
 set{
  _fileName = value;
  OnPropertyChanged("FileName");
}

The implementation of OnPropertyChanged and a simple explanation of INotifyPropertyChanged can be found at http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/RaisethePropertyChangedevent.htm

In your XAML, you already have a TextBlock which should be extended with a binding

TextBlock x:Name="tb_file" Text="{Binding Path=Filename}"

To ensure that the binding to a property in the code behind works, please refer to Binding objects defined in code-behind

Hope that helps (at least the first part of your question)

Community
  • 1
  • 1
SimonAx
  • 1,176
  • 1
  • 8
  • 32
  • @user3667841, if this answer has helped you to solve your problem, then please [accept it](http://stackoverflow.com/help/accepted-answer), as is customary on this website. – Sheridan Jun 11 '14 at 08:43