none
update database RRS feed

  • Question

  • I MAKE A PROJECT

    I HAVE MAKE A DATABASE WHICH HAS NCHAR CHARACTER AND ALSO A VARBINARY WHICH I USE TO UPLOAD PDF FILES 

    I AM OK WITH INSERT THE DATA BUT I HAVE PROBLEM WITH UPDATE THIS IS MY CODE:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;
    using System.Configuration;


    namespace Ηλεκτρονικό_Πρωτόκολλο
    {
        public partial class eiserxomena : Form
        {
            
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\home\Desktop\Ηλεκτρονικό Πρωτόκολλο\Ηλεκτρονικό Πρωτόκολλο\Hlektroniko_protokolo.mdf;Integrated Security=True;Connect Timeout=30");
            public eiserxomena()
            {
                InitializeComponent();
            }

            private void eiserxomena_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'hlektroniko_protokoloDataSet.eiserxomena' table. You can move, or remove it, as needed.
                this.eiserxomenaTableAdapter.Fill(this.hlektroniko_protokoloDataSet.eiserxomena);
                // TODO: This line of code loads data into the 'hlektroniko_protokoloDataSet.eiserxomena' table. You can move, or remove it, as needed.
                this.eiserxomenaTableAdapter.Fill(this.hlektroniko_protokoloDataSet.eiserxomena);
                // TODO: This line of code loads data into the 'hlektroniko_protokoloDataSet.eiserxomena' table. You can move, or remove it, as needed.
                this.eiserxomenaTableAdapter.Fill(this.hlektroniko_protokoloDataSet.eiserxomena);
                // TODO: This line of code loads data into the 'hlektroniko_protokoloDataSet.eiserxomena' table. You can move, or remove it, as needed.
                this.eiserxomenaTableAdapter.Fill(this.hlektroniko_protokoloDataSet.eiserxomena);


                disp_data();
            }
            public void disp_data()
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select [Αριθμός Πρωτοκόλλου],[Ημερομηνία Παραλαβής Εγγράφου],[Αριθμός Εισερχομένου Εγγράφου],[Τόπος που Εκδόθηκε],[Αρχή Έκδοσης],[Περίληψη],[Διεύθυνση που Εκδόθηκε],[Σχετικοί Αριθμοί],[Όνομα εγγράφου] from eiserxomena";
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                dataGridView_eiserxomena.DataSource = dt;
                pdfbox.src = "select Έγγραφο from eiserxomena";
                con.Close();

            }
            private void prosthiki_Click(object sender, EventArgs e)
            {
                con.Open();
                
                SqlCommand cmd = con.CreateCommand();
                
                cmd.CommandType = CommandType.Text;
                
                    
                    string filename = pdflocation;
                    FileStream strm = new FileStream(pdflocation, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(strm);
                    Byte[] size = br.ReadBytes((int)strm.Length);


                    cmd.CommandText = "insert into eiserxomena values (@arithmos_protokolou,@imerominia,@arithmos_egrafou,@topos,@arxi,@perilipsi,@dieuthinsi,@sxetikoi,@Data,@Name)";

                    cmd.Parameters.AddWithValue("@Name", filename);
                    cmd.Parameters.AddWithValue("@Data", size);


                    cmd.Parameters.AddWithValue("@arithmos_protokolou", textBox_arithmos_protokolou.Text);
                    cmd.Parameters.AddWithValue("@imerominia", dateTimePicker.Text);
                    cmd.Parameters.AddWithValue("@arithmos_egrafou", textBox_arithmos_eiserxomenou_egrafou.Text);
                    cmd.Parameters.AddWithValue("@topos", textBox_topos_ekdosis.Text);
                    cmd.Parameters.AddWithValue("@arxi", textBox_arxi_ekdosis.Text);
                    cmd.Parameters.AddWithValue("@perilipsi", textBox_perilipsi.Text);
                    cmd.Parameters.AddWithValue("@dieuthinsi", textBox_dieuthinsi.Text);
                    cmd.Parameters.AddWithValue("@sxetikoi", textBox_sxetikoi_arithmoi.Text);
                
                cmd.ExecuteNonQuery();
                con.Close();
                textBox_arithmos_protokolou.Text = "";
                dateTimePicker.Text = "";
                textBox_arithmos_eiserxomenou_egrafou.Text = "";
                textBox_topos_ekdosis.Text = "";
                textBox_arxi_ekdosis.Text = "";
                textBox_perilipsi.Text = "";
                textBox_dieuthinsi.Text = "";
                textBox_sxetikoi_arithmoi.Text = "";

                disp_data();
                MessageBox.Show("Έγινε επιτυχώς η καταχώρηση");
            }

            private void diagrafi_Click(object sender, EventArgs e)
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "delete from eiserxomena where όνομα ='" + textBox_arithmos_protokolou.Text + "'";
                cmd.ExecuteNonQuery();
                con.Close();
                disp_data();
                MessageBox.Show("Έγινε επιτυχώς η διαγραφή");
            }

            private void apothikeusi_Click(object sender, EventArgs e)
            {
                con.Open();

                SqlCommand cmd2 = con.CreateCommand();
                cmd2.CommandType = CommandType.Text;

                string command = string.Format(@"select Έγγραφο from eiserxomena where [Αριθμός Πρωτοκόλλου]='" + textBox_arithmos_protokolou.Text + "'", con);
                SqlCommand selectcommand = new SqlCommand(command, con);
                SqlDataReader reader = selectcommand.ExecuteReader();
                if (command == null)
                {
                    byte[] filedata = (byte[])reader[0];
                    string filename = ("select [Όνομα Εγγράφου] from eiserxomena where [Αριθμός Πρωτοκόλλου]=@arithmos_protokolou");
                    cmd2.Parameters.AddWithValue("@Name", filename);
                    cmd2.Parameters.AddWithValue("@Data", filedata);
                }
                con.Close();

                con.Open();

                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;

                cmd.CommandText = ("update eiserxomena set (@arithmos_protokolou,@imerominia,@arithmos_egrafou,@topos,@arxi,@perilipsi,@dieuthinsi,@sxetikoi,@data,@name) where [Αριθμός Πρωτοκόλλου]=@arithmos_protokolou");


                cmd.Parameters.AddWithValue("@arithmos_protokolou", textBox_arithmos_protokolou.Text);
                cmd.Parameters.AddWithValue("@imerominia", dateTimePicker.Text);
                cmd.Parameters.AddWithValue("@arithmos_egrafou", textBox_arithmos_eiserxomenou_egrafou.Text);
                cmd.Parameters.AddWithValue("@topos", textBox_topos_ekdosis.Text);
                cmd.Parameters.AddWithValue("@arxi", textBox_arxi_ekdosis.Text);
                cmd.Parameters.AddWithValue("@perilipsi", textBox_perilipsi.Text);
                cmd.Parameters.AddWithValue("@dieuthinsi", textBox_dieuthinsi.Text);
                cmd.Parameters.AddWithValue("@sxetikoi", textBox_sxetikoi_arithmoi.Text);

                cmd.ExecuteNonQuery();
                con.Close();
                disp_data();
                MessageBox.Show("Έγινε επιτυχώς η αποθήκευση");
            }

            private void provoli_Click(object sender, EventArgs e)
            {
                disp_data();
            }

            private void anazitisi_Click(object sender, EventArgs e)
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select [Αριθμός Πρωτοκόλλου],[Ημερομηνία Παραλαβής Εγγράφου],[Αριθμός Εισερχομένου Εγγράφου],[Τόπος που Εκδόθηκε],[Αρχή Έκδοσης],[Περίληψη],[Διεύθυνση που Εκδόθηκε],[Σχετικοί Αριθμοί],[Όνομα εγγράφου] from eiserxomena where [Αριθμός Πρωτοκόλλου] LIKE '%" + textBox_search.Text+ "%' or [Ημερομηνία Παραλαβής Εγγράφου] LIKE '%" + textBox_search.Text + "%' or [Αριθμός Εισερχομένου Εγγράφου] LIKE '%" + textBox_search.Text + "%' or [Τόπος που Εκδόθηκε] LIKE '%" + textBox_search.Text + "%' or [Αρχή Έκδοσης] LIKE '%" + textBox_search.Text + "%' or [Περίληψη] LIKE '%" + textBox_search.Text + "%' or [Διεύθυνση που Εκδόθηκε] LIKE '%" + textBox_search.Text + "%' or [Σχετικοί Αριθμοί] LIKE '%" + textBox_search.Text + "%'";
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                dataGridView_eiserxomena.DataSource = dt;
               
                con.Close();



                





            }
            string pdflocation = "";
            private void browse_Click(object sender, EventArgs e)
            {
                
                OpenFileDialog dlg = new OpenFileDialog();
                DialogResult dlgRes = dlg.ShowDialog();
                if (dlgRes != DialogResult.Cancel)
                {
                    pdflocation = dlg.FileName.ToString();
                   
                    pdfbox.src = pdflocation;                        
                }
                
                

            }

            private void dataGridView_eiserxomena_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {

            }

            private void eiserxomenaBindingSource_CurrentChanged(object sender, EventArgs e)
            {

            }
        }

           
    }
       

    Friday, March 3, 2017 10:56 PM

All replies

  • Hello,

    Please pinpoint where you are having issue and also please place your code into code blocks that shows only relevant code to the issue.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 4, 2017 12:03 AM
    Moderator
  • As Karen said, please put your code into a code block for Highlighting.

    Also "does not work" is not a sufficient problem description.
    What do you expect to happen?
    What is happening instead?
    Are there any .NET or SQL Exceptions?
    What exact lines are the exceptions comming from?


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Saturday, March 4, 2017 7:05 PM
  • Hi kalukas,

    Thank you for posting here.

    For your question, could you provide more details? Error message would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 6, 2017 8:19 AM
    Moderator