Sunday 4 January 2015

C# OPEN FILE DIALOG BROWSE FILES

       copy and pest
      inside a method  eg button click
   private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result=dialog.ShowDialog();
            if(result==DialogResult.OK)
            {
                buff=System.IO.File.ReadAllBytes(dialog.FileName);
                labelfilename.Text=dialog.FileName;
            }
        }

otherwise

OpenFileDialog dialog;       
 DialogResult result=dialog.ShowDialog();
            if(result==DialogResult.OK)
            {
                buff=System.IO.File.ReadAllBytes(dialog.FileName);
                labelfilename.Text=dialog.FileName;
            }

C# Retrieving Values From A local Database

i assume you have already watched the previous tutorial

if you havent then check this out
http://icytrey.blogspot.com/2015/01/c-easiest-way-to-connect-to-local.html
and can connect to your local database without any problems

just copy and paste its not a crime



   System.Data.SqlClient.SqlConnection con;
            con = new System.Data.SqlClient.SqlConnection();

            con.ConnectionString = @"Data Source=(LocalDB)\v11.0;
                          AttachDbFilename=C:\Users\icytrey\documents\visual studio 2013\Projects\PdfToWordDocument\PdfToWordDocument\MysimpleDatabase.mdf;
                          Integrated Security=True;
                          Connect Timeout=30;";
            con.Open();
            SqlCommand command = new SqlCommand("select * from Movies", con);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                int Id = reader.GetInt32(0);
                String name = reader.GetString(1);
                String categoruy= reader.GetString(2);
                listBox1.Items.Add(name);

            }

          
            con.Close();

C# EASIEST WAY TO CONNECT TO A LOCAL DATABASE

i spent close to one full year doing this till i suceded and thought of making a simple tutorial that can help alot of my brothers out there
Follow These steps and you will insert to a database



1  First all right click on your project
as shown below





then add new item



step 3 click add new item and select Service based database i called mine blogger.mdf


step 4 click add and wait for the system to create after creation
double click on the database

this will open the database in the server explorer located on the top left corner of visual studio 2013
as shown below
then right click on the  as shown below and choose  modify


step 5 you will see this

change the data source to Microsoft SQL Server Database File (SqlClient)
and locate your database
after this



This is where we all get it wrong the data source is not always .\SQLEXPRESS as most tutorials give

click advanced and you will be greeted with all the information you are going to need to connect to the database

 con.ConnectionString = @"Data Source=(LocalDB)\v11.0;
                          AttachDbFilename=C:\Users\icytrey\documents\visual studio 2013\Projects\PdfToWordDocument\PdfToWordDocument\MysimpleDatabase.mdf;
                          Integrated Security=True;
                          Connect Timeout=30;";

note  AttachDbFilename locate your database.mdf file in the path



then you are almost done


here is the complete code


            System.Data.SqlClient.SqlConnection con;
            con = new System.Data.SqlClient.SqlConnection();
           
            con.ConnectionString = @"Data Source=(LocalDB)\v11.0;
                          AttachDbFilename=C:\Users\icytrey\documents\visual studio 2013\Projects\PdfToWordDocument\PdfToWordDocument\MysimpleDatabase.mdf;
                          Integrated Security=True;
                          Connect Timeout=30;";

            con.Open();
            SqlCommand addSite = new SqlCommand(@"INSERT INTO Movies (movie,category) VALUES (@name,@cat)", con);
            addSite.Parameters.AddWithValue("@name", "X men ");
            addSite.Parameters.AddWithValue("@cat", "Action");
            addSite.ExecuteNonQuery();

            //Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\icytrey\Documents\Visual Studio 2013\Projects\PdfToWordDocument\PdfToWordDocument\MysimpleDatabase.mdf";Integrated Security=True;Connect Timeout=30
           
            MessageBox.Show("Data Inserted");
            con.Close();


create the table by expanding the database right click on Tables as shown below

last step you will be greeted with this interface create the table and hit update icon

and you are done


dont forget to make id IDENTITY PRIMARY KEY

thanks hope this helps you alot