Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.IO;

namespace hrms_hrmodule
{
    /// <summary>
    /// Interaction logic for Register.xaml
    /// </summary>
    public partial class Register : Window
    {
        public SqlConnection con;
        public SqlCommand cmd;
        public byte[] doc1, doc2, pic1;
        public string sex;
        public Register()
        {
            InitializeComponent();

        }
        private void load_data(object sender, RoutedEventArgs e)
        {


        }

        private void back(object sender, RoutedEventArgs e)
        {
            MainWindow main = new MainWindow();
            this.Hide();
            main.Show();
        }
        public void loadimage(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.ShowDialog();
            FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
            Byte[] pic1 = new Byte[fs.Length];
            fs.Read(pic1, 0, Convert.ToInt32(fs.Length));
            text_photo.Text = dlg.SafeFileName.ToString();


        }
        public void loaddoc1(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog flg = new Microsoft.Win32.OpenFileDialog();
            flg.ShowDialog();
            FileStream fd = new FileStream(flg.FileName, FileMode.Open, FileAccess.Read);
            Byte[] doc1 = new Byte[fd.Length];
            fd.Read(doc1, 0, Convert.ToInt32(fd.Length));       
            text_doc1.Text = flg.SafeFileName.ToString();
        }
        public void loaddoc2(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog fl = new Microsoft.Win32.OpenFileDialog();
            fl.ShowDialog();
            FileStream fg = new FileStream(fl.FileName, FileMode.Open, FileAccess.Read);
            
            Byte[] doc2 = new Byte[fg.Length];
            fg.Read(doc2, 0, Convert.ToInt32(fg.Length));
            text_doc2.Text = fl.SafeFileName.ToString();
        }
        public void savedata(object sender, RoutedEventArgs e)
        {
            con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\Software\Hrms\hrms_hrmodule\hrms_hrmodule\employee.mdf;Integrated Security=True;");
            try
            {
                con.Open();
                string s = "insert into Registration values(@empid,@name,@age,@dob,@add,@contact,@sex,@skill,@prof,@photo,@doc1,@doc2)";

                cmd.Parameters.Add(new SqlParameter("@empid",SqlDbType.Int));
                cmd.Parameters.Add(new SqlParameter("@name",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@age", SqlDbType.Int));
                cmd.Parameters.Add(new SqlParameter("@dob", SqlDbType.Date));
                cmd.Parameters.Add(new SqlParameter("@add",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@contact",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@sex",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@skill",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@prof",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@photo",SqlDbType.Image));
                cmd.Parameters.Add(new SqlParameter("@doc1",SqlDbType.Image));
                cmd.Parameters.Add(new SqlParameter("@doc2", SqlDbType.Image));

                cmd.Parameters["@empid"].Value = text_id.Text;
                cmd.Parameters["@name"].Value = text_name.Text;
                cmd.Parameters["@age"].Value = text_age.Text;
                cmd.Parameters["@add"].Value = text_address.Text;
                cmd.Parameters["@contact"].Value = text_contact.Text;
                cmd.Parameters["@dob"].Value = datepicker.Text;
                cmd.Parameters["@prof"].Value = comboBox1.Text;
                cmd.Parameters["@skill"].Value =comboBox.Text;
                cmd.Parameters["@photo"].Value = pic1;
                cmd.Parameters["@doc1"].Value = doc1;
                cmd.Parameters["@doc2"].Value = doc2;
                cmd.Parameters["@sex"].Value = combo_sex.Text;
                cmd = new SqlCommand(s,con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("record saved");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
          }
             
    
 
    }
}
Posted
Comments
Member 12003400 6-Oct-15 9:19am    
put a breakpoint and check where does it's giving error,it will give you exact object which is throwing null reference error.
Richard Deeming 6-Oct-15 9:47am    
REPOST
You have already posted this question:
http://www.codeproject.com/Questions/1036154/my-code-for-saving-image-in-sql-server-but-the-cod?arn=0[^]

You have clearly ignored the answer you were given yesterday, despite accepting it as the solution.

The answer hasn't changed in the last 24 hours.

Have you debug the code.? Which line you got this error..? Object reference error is getting when you are trying to do action using null reference at that time you go this type of error, debug the code and check which line you got this error and then check that whether instance contains value or not.
 
Share this answer
 
Without running your code, we can't be sure - but if you run it in the debugger it will stop on the line that causes the problem and it should be obvious, particularly if you use the debugger to look at the variables and what they contain.

But...I'd guess it's your "cmd" object - I don't see anything in that code which gives it a value at all...

Personally, I wouldn't declare the Connection or Command objects as class level - keep them local and use a using block to ensure they are Disposed properly:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand(" ... ", con))
        {
        ...
That also has the advantage that you can't use it unless it has a value, because it goes out of scope at the end of the using block and the compiler will complain if you try to use it.
 
Share this answer
 
Comments
Richard Deeming 6-Oct-15 9:49am    
It's almost certainly still the pic1 object you helped him with yesterday[^].

He's accepted your solution, and then completely ignored it and reposted the question. :(
OriginalGriff 6-Oct-15 10:02am    
You do have to wonder if you are talking to the wall sometimes...:sigh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900