必须声明标量变量@ 12 [英] Must declare the scalar variable @12

查看:83
本文介绍了必须声明标量变量@ 12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnSave_Click(object sender, EventArgs e)
        {
           
            try
            {
                if (txtFirstName.Text == "")
                {
                    MessageBox.Show("Please enter First Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtFirstName.Focus();
                    return;
                }
                if (txtLastName.Text == "")
                {
                    MessageBox.Show("Please enter Last Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtLastName.Focus();
                    return;
                }
                //if (txtMobileNo.Text == "")
                //{
                  //  MessageBox.Show("Please enter mobile number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   // txtMobileNo.Focus();
                    //return;
               // }
                cc.con = new SqlConnection(cs.DBCon);
                cc.con.Open();
                string ct = "Select * from StaffMain where StaffCode=@d1";
                cc.cmd = new SqlCommand(ct);
                cc.cmd.Connection = cc.con;
                cc.cmd.Parameters.AddWithValue("@d1", txtStaffCode.Text);
                cc.rdr = cc.cmd.ExecuteReader();
                if (cc.rdr.Read()) 
                {
                    MessageBox.Show("Staff Code already exit", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtStaffCode.Text = "";
                    txtStaffCode.Focus();
                    if (cc.rdr != null) 
                    {
                        cc.con.Close();
                    }
                         return;
                }
                cc.con = new SqlConnection(cs.DBCon);
                cc.con.Open();
                string cb = ("insert into StaffMain(StaffCode,FirstName,LastName,FatherFname,FatherLname,CitizenNo,District,BankAcc,BankName,PermanentAddress,TempAddress,PhoneI,PhoneII,MobileNo,Email,Department,Designation,Groups,Status,Remarks)VALUES(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@12,@d13,@d14,@d15,@d16,@d17,@d18,@d19,@d20)");
                cc.cmd = new SqlCommand(cb);
                cc.cmd.Connection = cc.con;
                cc.cmd.Parameters.AddWithValue("@d1", txtStaffCode.Text);
                cc.cmd.Parameters.AddWithValue("@d2", txtFirstName.Text);
                cc.cmd.Parameters.AddWithValue("@d3", txtLastName.Text);
                cc.cmd.Parameters.AddWithValue("@d4", txtFatherFname.Text);
                cc.cmd.Parameters.AddWithValue("@d5", txtFatherLname.Text);
                cc.cmd.Parameters.AddWithValue("@d6", txtCitizen.Text);
                cc.cmd.Parameters.AddWithValue("@d7", cmbDistrict.Text);
                cc.cmd.Parameters.AddWithValue("@d8", txtBankAcc.Text);
                cc.cmd.Parameters.AddWithValue("@d9", cmbBankName.Text);
                cc.cmd.Parameters.AddWithValue("@d10", txtPermanentAddress.Text);
                cc.cmd.Parameters.AddWithValue("@d11", txtTemporaryAddress.Text);
                cc.cmd.Parameters.AddWithValue("@d12", txtPhoneI.Text);
                cc.cmd.Parameters.AddWithValue("@d13", txtPhoneII.Text);
                cc.cmd.Parameters.AddWithValue("@d14", txtMobileNo.Text);
                cc.cmd.Parameters.AddWithValue("@d15", txtEmailID.Text);
                cc.cmd.Parameters.AddWithValue("@d16", txtDepartment.Text);
                cc.cmd.Parameters.AddWithValue("@d17", txtDesiganation.Text);
                cc.cmd.Parameters.AddWithValue("@d18", txtGroup.Text);
                cc.cmd.Parameters.AddWithValue("@d19", cmbStatus.Text);
                cc.cmd.Parameters.AddWithValue("@d20", txtRemarks.Text);
                cc.cmd.ExecuteReader();
                cc.con.Close();
                st1 = lblUser.Text;
                st2 = "Added the new Staff having StaffCode'" + txtStaffCode.Text + "'";
                MessageBox.Show("Data Added Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            
        }



[edit]已添加代码块 - OriginalGriff [/ edit]



我尝试过:



必须声明标量变量


[edit]Code block added - OriginalGriff[/edit]

What I have tried:

must declare the scalar variable

推荐答案

查看您的代码:

Look at your code:
string cb = ("insert into StaffMain(StaffCode,...,Remarks)VALUES(@d1,...,@d11,@12,@d13,...,@d20)");
...
cc.cmd.Parameters.AddWithValue("@d12", txtPhoneI.Text);

名称需要匹配:@ 12和@ d12不一样。

为这个值命名参数是个更好的主意:

而不是@ d1,使用@SC代表StaffCode。

而不是@ d12,使用PHI作为Phone I。

依此类推 - 当你阅读它时,它会更加明显。

The names need to match:@12 and @d12 are not the same.
It's a better idea to name your parameters for the value:
Instead of "@d1", use "@SC" for "StaffCode".
Instead of "@d12", use "PHI" for "Phone I".
And so on - it's a lot more obvious when you read it.


这篇关于必须声明标量变量@ 12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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