Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

.net - No value given for one or more required parameters.C# MS Access

i have a Winform App that stores about 19 textbox data in an Access Database.. as far as i can tell, i'm entering all possible data in them before executing nonquery command...

here is my command text

subcom.CommandText = @"Insert Into GOA_DB (Srno, Branch_Code, Branch_Name, Node, 
       Region, Date_Comm, Address, MPLS, Leased_Line, LL_IP,  Router_WAN_IP, Router_LAN_IP, 
       Distance_from_RCC, OATM_Parent_details, Curcuit_ID, Circuit_Type, G703_used, G703_srno,
       V35_used, V35_srno, Bandwidth, Router_Make_Model, Router_srno, Router_vendor, 
       Backup_link, BckUP_Vendor, BcKup_WAN_IP ) Values (@Srno, @Branch_Code, @Branch_Name, 
       @Node, @Region, @Date_Comm, @Address, @MPLS, @Leased_Line, @LL_IP, @Router_WAN_IP, 
       @Router_LAN_IP, @Distance_from_RCC,  @OATM_Parent_details, @Curcuit_ID, 
       @Circuit_Type,  @G703_used, @G703_srno, @V35_used, @V35_srno, @Bandwidth, 
       @Router_Make_Model, @Router_srno, @Router_vendor, @Backup_link, @BckUP_Vendor, 
       @BcKup_WAN_IP )";

here are my parameters.

subcom.Parameters.AddWithValue(@"Srno", label26.Text);
        subcom.Parameters.AddWithValue(@"Branch_Code", textBox1.Text);
        subcom.Parameters.AddWithValue(@"Branch_Name", textBox2.Text);
        subcom.Parameters.AddWithValue(@"Node", textBox3.Text);
        subcom.Parameters.AddWithValue(@"Region", textBox4.Text);
        subcom.Parameters.AddWithValue(@"Date_Comm", dateTimePicker1.Text);
        subcom.Parameters.AddWithValue(@"Address", textBox5.Text);


        if (radioButton1.Checked)
        {
            string yes = "Yes";
            subcom.Parameters.AddWithValue(@"MPLS", @yes);
            subcom.Parameters.AddWithValue(@"Leased_Line", textBox15.Text);
            subcom.Parameters.AddWithValue(@"LL_IP", textBox15.Text);
        }

        if (radioButton2.Checked)
        {
            string Y = "Yes";
            string na1 = "Not Applicable";
            subcom.Parameters.AddWithValue(@"Leased_Line", @Y);
            subcom.Parameters.AddWithValue(@"MPLS", @na1);
            subcom.Parameters.AddWithValue(@"LL_IP", textBox15.Text);
        }


        subcom.Parameters.AddWithValue(@"Router_WAN_IP", textBox8.Text);
        subcom.Parameters.AddWithValue(@"Router_LAN_IP", textBox9.Text);
        subcom.Parameters.AddWithValue(@"Distance_from_RCC", textBox6.Text);
        subcom.Parameters.AddWithValue(@"OATM_Parent_details", textBox7.Text);
        subcom.Parameters.AddWithValue(@"Curcuit_ID", textBox10.Text);

        if (radioButton3.Checked)
        {

            string ml = "MLLN";
            string No = "No";
            subcom.Parameters.AddWithValue(@"Circuit_Type", radioButton3.Text);
            subcom.Parameters.AddWithValue(@"G703_used", No);
            subcom.Parameters.AddWithValue(@"V35_used", No);
            subcom.Parameters.AddWithValue(@"V35_used", @No);
            subcom.Parameters.AddWithValue(@"V35_srno", textBox17.Text);
        }
        //to enter non-mlln details
        if (radioButton4.Checked)
        {
            string yes3 = "Yes";
            //string non = "Non-MLLN";
            subcom.Parameters.AddWithValue(@"Circuit_Type", radioButton4.Text);
            subcom.Parameters.AddWithValue(@"G703_used", yes3);
            subcom.Parameters.AddWithValue(@"G703_srno", textBox16.Text);
            subcom.Parameters.AddWithValue(@"V35_used", yes3);
            subcom.Parameters.AddWithValue(@"V35_srno", textBox17.Text);

        }

        subcom.Parameters.AddWithValue(@"Bandwidth", comboBox1.Text);
        subcom.Parameters.AddWithValue(@"Router_Make_Model", textBox13.Text);
        subcom.Parameters.AddWithValue(@"Router_srno", textBox14.Text);
        subcom.Parameters.AddWithValue(@"Router_vendor", textBox12.Text);

        if (radioButton5.Checked)
        {

            subcom.Parameters.AddWithValue(@"Backup_link", radioButton5.Text);
            subcom.Parameters.AddWithValue(@"BckUp_vendor", textBox18.Text);
            subcom.Parameters.AddWithValue(@"Bck_WAN_IP", textBox19.Text);

        }
        if (radioButton6.Checked)
        {


            subcom.Parameters.AddWithValue(@"Backup_link", radioButton6.Text);
            subcom.Parameters.AddWithValue(@"BckUp_vendor", textBox18.Text);
            subcom.Parameters.AddWithValue(@"Bck_WAN_IP", textBox19.Text);
        }


if (null2(this))
          {
             // MessageBox.Show("Some empty values are present");
              try
              {
                  int resul = subcom.ExecuteNonQuery();
                  if (resul > 0)
                  {
                      MessageBox.Show("Entered Successfully");
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }

         }

Here the exception thrown is No value given for one or more required parameters... execution stops... any clue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Besides the errors in the code, You can not condition their addition of parameters after they are in the query statement. That is, all parameters who are in the INSERT statement must be in Command Parameters.

In OleDbCommand even greater problem, That the parameters are identified by their position index, not by their name.

Try this way:

subcom.CommandText = "Insert Into GOA_DB (Srno, Branch_Code, Branch_Name, Node, Region, Date_Comm, Address, MPLS, Leased_Line, LL_IP,  Router_WAN_IP, Router_LAN_IP, Distance_from_RCC, OATM_Parent_details, Curcuit_ID, Circuit_Type, G703_used, G703_srno, V35_used, V35_srno, Bandwidth, Router_Make_Model, Router_srno, Router_vendor, Backup_link, BckUP_Vendor, BcKup_WAN_IP ) Values" +
                                         "(@Srno, @Branch_Code, @Branch_Name, @Node, @Region, @Date_Comm, @Address, @MPLS, @Leased_Line, @LL_IP, @Router_WAN_IP, @Router_LAN_IP, @Distance_from_RCC,  @OATM_Parent_details, @Curcuit_ID, @Circuit_Type,  @G703_used, @G703_srno, @V35_used, @V35_srno, @Bandwidth, @Router_Make_Model, @Router_srno, @Router_vendor, @Backup_link, @BckUP_Vendor, @BcKup_WAN_IP )";

var strParams = "@Srno,@Branch_Code,@Branch_Name,@Node,@Region,@Date_Comm,@Address,@MPLS,@Leased_Line,@LL_IP,@Router_WAN_IP,@Router_LAN_IP,@Distance_from_RCC,@OATM_Parent_details,@Curcuit_ID,@Circuit_Type,@G703_used,@G703_srno,@V35_used,@V35_srno,@Bandwidth,@Router_Make_Model,@Router_srno,@Router_vendor,@Backup_link,@BckUP_Vendor,@BcKup_WAN_IP";

subcom.Parameters.AddRange(strParams.Split(',').Select(x => new SqlParameter(x, SqlDbType.NVarChar)).ToArray());

string yes = "Yes";
string No = "No";
string na1 = "Not Applicable";

subcom.Parameters["@Srno"].Value =  label26.Text;
subcom.Parameters["@Branch_Code"].Value =  textBox1.Text;
subcom.Parameters["@Branch_Name"].Value =  textBox2.Text;
subcom.Parameters["@Node"].Value =  textBox3.Text;
subcom.Parameters["@Region"].Value =  textBox4.Text;
subcom.Parameters["@Date_Comm"].Value =  dateTimePicker1.Text;
subcom.Parameters["@Address"].Value =  textBox5.Text;

subcom.Parameters["@Router_WAN_IP"].Value =  textBox8.Text;
subcom.Parameters["@Router_LAN_IP"].Value =  textBox9.Text;
subcom.Parameters["@Distance_from_RCC"].Value =  textBox6.Text;
subcom.Parameters["@OATM_Parent_details"].Value =  textBox7.Text;
subcom.Parameters["@Curcuit_ID"].Value =  textBox10.Text;


subcom.Parameters["@Bandwidth"].Value =  comboBox1.Text;
subcom.Parameters["@Router_Make_Model"].Value =  textBox13.Text;
subcom.Parameters["@Router_srno"].Value =  textBox14.Text;
subcom.Parameters["@Router_vendor"].Value =  textBox12.Text;


if (radioButton1.Checked)
{
    subcom.Parameters["@MPLS"].Value = yes;
    subcom.Parameters["@Leased_Line"].Value =  textBox15.Text;
    subcom.Parameters["@LL_IP"].Value =  textBox15.Text;
}
if (radioButton2.Checked)
{
    subcom.Parameters["@Leased_Line"].Value =  yes;
    subcom.Parameters["@MPLS"].Value =  na1;
    subcom.Parameters["@LL_IP"].Value =  textBox15.Text;
}
if (radioButton3.Checked)
{
    string ml = "MLLN";
    subcom.Parameters["@Circuit_Type"].Value =  radioButton3.Text;
    subcom.Parameters["@G703_used"].Value =  No;
    subcom.Parameters["@V35_used"].Value =  No;
    subcom.Parameters["@V35_used"].Value =  No;
    subcom.Parameters["@V35_srno"].Value =  textBox17.Text;
}
//to enter non-mlln details
if (radioButton4.Checked)
{
    //string non = "Non-MLLN";
    subcom.Parameters["@Circuit_Type"].Value =  radioButton4.Text;
    subcom.Parameters["@G703_used"].Value =  yes;
    subcom.Parameters["@G703_srno"].Value =  textBox16.Text;
    subcom.Parameters["@V35_used"].Value =  yes;
    subcom.Parameters["@V35_srno"].Value =  textBox17.Text;
}
if (radioButton5.Checked)
{
    subcom.Parameters["@Backup_link"].Value =  radioButton5.Text;
    subcom.Parameters["@BckUp_vendor"].Value =  textBox18.Text;
    subcom.Parameters["@Bck_WAN_IP"].Value =  textBox19.Text;
}
if (radioButton6.Checked)
{
    subcom.Parameters["@Backup_link"].Value =  radioButton6.Text;
    subcom.Parameters["@BckUp_vendor"].Value =  textBox18.Text;
    subcom.Parameters["@Bck_WAN_IP"].Value =  textBox19.Text;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...