Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;

namespace Login_Form
{
    public partial class frmCreateCompany : Form
    {
        SqlDataReader rdr = null;
        DataTable dtable = new DataTable();
        SqlConnection con = null;

        SqlCommand cmd = null;
        DataTable dt = new DataTable();
        string cs = "Data Source=.\\SqlExpress; Integrated Security=True; AttachDbFilename=|DataDirectory|\\Garment.mdf; User Instance=true;";
        public frmCreateCompany()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

            //Autocomplete();
        }

        private void button4_Click(object sender, EventArgs e)
        {

            if (textBox2.Text == "")
            {
                MessageBox.Show("Please enter Company Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox2.Focus();
                return;
            }
            if (textBox3.Text == "")
            {
                MessageBox.Show("Please enter Mailing  Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox3.Focus();
                return;
            }
            if (textBox4.Text == "")
            {
                MessageBox.Show("Please enter License Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox4.Focus();
                return;
            }
            if (textBox5.Text == "")
            {
                MessageBox.Show("Please enter Post Offcie Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox5.Focus();
                return;
            }
            if (textBox6.Text == "")
            {
                MessageBox.Show("Please enter Location", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox6.Focus();
                return;
            }
            if (textBox7.Text == "")
            {
                MessageBox.Show("Please enter State", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox7.Focus();
                return;
            }
            if (textBox8.Text == "")
            {
                MessageBox.Show("Please enter Country", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox8.Focus();
                return;
            }
            if (textBox9.Text == "")
            {
                MessageBox.Show("Please enter Telephone Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox9.Focus();
                return;
            }
            if (textBox10.Text == "")
            {
                MessageBox.Show("Please enter Fax No.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox10.Focus();
                return;
            }
            if (textBox11.Text == "")
            {
                MessageBox.Show("Please enter Email id", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox11.Focus();
                return;
            }
            if (textBox12.Text == "")
            {
                MessageBox.Show("Please enter Website Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox12.Focus();
                return;
            }
            if (textBox13.Text == "")
            {
                MessageBox.Show("Please enter Currency Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox13.Focus();
                return;
            }

            try
            {
                con = new SqlConnection(cs);
                con.Open();
                string cb = "INSERT INTO COMPANY_MASTER(C_Name,M_Name,L_No,Tax_No,PO_No,Location,State,Country,Telephone,Fax_No,Email_Id,Website,Currency_Name,Company_logo,APT,APF) VALUES(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16)";
                cmd = new SqlCommand(cb);
                cmd.Connection = con;
                cmd.Parameters.Add(new SqlParameter("@d1", System.Data.SqlDbType.NChar, 10, "C_Name"));
                cmd.Parameters.Add(new SqlParameter("@d2", System.Data.SqlDbType.NChar, 10, "M_Name"));
                cmd.Parameters.Add(new SqlParameter("@d3", System.Data.SqlDbType.NChar, 10, "L_No"));
                cmd.Parameters.Add(new SqlParameter("@d4", System.Data.SqlDbType.NChar, 10, "Tax_No"));
                cmd.Parameters.Add(new SqlParameter("@d5", System.Data.SqlDbType.NChar, 10, "PO_No"));
                cmd.Parameters.Add(new SqlParameter("@d6", System.Data.SqlDbType.NChar, 10, "Location"));
                cmd.Parameters.Add(new SqlParameter("@d7", System.Data.SqlDbType.NChar, 10, "State"));
                cmd.Parameters.Add(new SqlParameter("@d8", System.Data.SqlDbType.NChar, 10, "Country"));
                cmd.Parameters.Add(new SqlParameter("@d9", System.Data.SqlDbType.NChar, 10, "Telephone"));
                cmd.Parameters.Add(new SqlParameter("@d10", System.Data.SqlDbType.NChar, 10, "Fax_No"));
                cmd.Parameters.Add(new SqlParameter("@d11", System.Data.SqlDbType.NChar, 10, "email_Id"));
                cmd.Parameters.Add(new SqlParameter("@d12", System.Data.SqlDbType.NChar, 10, "Website"));
                cmd.Parameters.Add(new SqlParameter("@d13", System.Data.SqlDbType.NChar, 10, "Currency_Name"));
                MemoryStream ms = new MemoryStream();
                Bitmap bmpImage = new Bitmap(pictureBox1.Image);
                bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                byte[] data = ms.GetBuffer();
                SqlParameter p = new SqlParameter("@d14", SqlDbType.Image);
                p.Value = data;
                cmd.Parameters.Add(p);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Add(new SqlParameter("@d15", System.Data.SqlDbType.Date, 30, "APT"));
                cmd.Parameters.Add(new SqlParameter("@d16", System.Data.SqlDbType.Date, 30, "APF"));

                cmd.Parameters["@d1"].Value = textBox2.Text.Trim();
                cmd.Parameters["@d2"].Value = textBox3.Text.Trim();
                cmd.Parameters["@d3"].Value = textBox4.Text.Trim();
                cmd.Parameters["@d4"].Value = textBox5.Text.Trim();
                cmd.Parameters["@d5"].Value = textBox6.Text.Trim();
                cmd.Parameters["@d6"].Value = textBox7.Text.Trim();
                cmd.Parameters["@d7"].Value = textBox8.Text.Trim();
                cmd.Parameters["@d8"].Value = textBox9.Text.Trim();
                cmd.Parameters["@d9"].Value = textBox10.Text.Trim();
                cmd.Parameters["@d10"].Value = textBox11.Text.Trim();
                cmd.Parameters["@d11"].Value = textBox12.Text.Trim();
                cmd.Parameters["@d12"].Value = textBox13.Text.Trim();
                cmd.Parameters["@d13"].Value = textBox14.Text.Trim();
                cmd.Parameters["@d14"].Value = pictureBox1.Image;
                cmd.Parameters["@d15"].Value = AccountingStart.Text.Trim();
                cmd.Parameters["@d16"].Value = AccountingFrom.Text.Trim();
                cmd.ExecuteReader();
                con.Close();
                MessageBox.Show("Successfully saved ", "Company", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            var _with1 = openFileDialog1;

            _with1.Filter = ("Images |*.png; *.bmp; *.jpg;*.jpeg; *.gif; *.ico");
            _with1.FilterIndex = 4;

            //Reset the file name
            openFileDialog1.FileName = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image=Image.FromFile(openFileDialog1.FileName);
               
            }
        }
        }
    }


                //string cb = "INSERT INTO Company_Master(C_Name,M_Name,L_No,Tax_No,PO_No,Location,State,Country,Telephone,Fax_No,Email_Id,Website,Currency_Name,Company_logo,APT,APF) VALUES('" + textBox2.Text + "', '" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + pictureBox1.Image + "','" + dateTimePicker1.Value + "','" + dateTimePicker2.Value + "')";

                //SqlConnection myConnection = default(SqlConnection);
                //myConnection = new SqlConnection("Data Source=.\\SqlExpress; Integrated Security=True; AttachDbFilename=|DataDirectory|\\Garment.mdf; User Instance=true;");

                //SqlCommand myCommand = default(SqlCommand);
                //myCommand = new SqlCommand("INSERT INTO Company_Master(C_Name,M_Name,L_No,Tax_No,PO_No,Location,State,Country,Telephone,Fax_No,Email_Id,Website,Currency_Name,Company_logo,APT,APF) VALUES('" + textBox2.Text + "', '" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + pictureBox1.Image + "','" + dateTimePicker1.Value + "','" + dateTimePicker2.Value + "')", myConnection);
                
                //myCommand.Connection.Open();
                //SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            


       
    
<pre lang="c#"><pre lang="c#">
Posted

You're executing the query before adding the remaining parameters(@d15 & @d16), this portion
C#
cmd.ExecuteNonQuery();//<---------------------------------------This line
cmd.Parameters.Add(new SqlParameter("@d15", System.Data.SqlDbType.Date, 30, "APT"));
cmd.Parameters.Add(new SqlParameter("@d16", System.Data.SqlDbType.Date, 30, "APF"));

So it gives you the error. Remove that.
 
Share this answer
 
Comments
Ankur\m/ 22-Apr-14 3:23am    
Oh, I missed that. Param 15 was correct so i thought the error has to be jsut before that. 5 to you.
thatraja 22-Apr-14 3:28am    
Thank you, get a virtual Coffee! :D
You will need to pass byte array (byte []) type to your parameter @d14. This post will help you convert image to byte array - C# Image to Byte Array and Byte Array to Image Converter Class[^].
Hope that helps!
 
Share this answer
 
Comments
Er Aslam Khan 22-Apr-14 2:59am    
its error is not related to image related error it is related to date time
Ankur\m/ 22-Apr-14 3:26am    
See the other answer. You are executing the query before adding the parameters.

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