在此代码中,错误必须声明缩放器变量“@ d15”..请帮助我 [英] In this code error is must declare scaler variable "@d15".. please help me

查看:78
本文介绍了在此代码中,错误必须声明缩放器变量“@ d15”..请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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#">

推荐答案

您正在执行查询在添加剩余参数之前(@ d15& @ d16),这部分

You're executing the query before adding the remaining parameters(@d15 & @d16), this portion
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.


您需要将字节数组(byte [])类型传递给参数@ d14。这篇文章将帮助您将图像转换为字节数组 - C#图像到字节数组和字节数组到图像转换器类 [ ^ ]。

希望有所帮助!
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!


这篇关于在此代码中,错误必须声明缩放器变量“@ d15”..请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆