必须声明标量变量& quot; @ filequestion&安培; QUOT; 。编译下面的代码plz时遇到这个错误帮我解决了 [英] Must declare the scalar variable " @filequestion" . Am facing this error while compiling below code plz help me solve out

查看:105
本文介绍了必须声明标量变量& quot; @ filequestion&安培; QUOT; 。编译下面的代码plz时遇到这个错误帮我解决了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button2_Click(object sender, EventArgs e)
   {
       string conn4 = ConfigurationManager.ConnectionStrings["abc"].ConnectionString;
           SqlConnection con4 = new SqlConnection(conn4);
          con4.Open();
          SqlCommand cmd4 = new SqlCommand("insert into AddQuestions(testcategory,questiontype,answertype,optiontype,filequestion,textquestion,course,correctscore,incorrectscore,questiontime,displayorder) values(@testcategory,@questiontype,@answertype,@optiontype,@filequestion,@textquestion,@course,@correctscore,@incorrectscore,@questiontime,@displayorder)", con4);

          cmd4.Parameters.AddWithValue("@testcategory", DropDownList1.SelectedValue);
          cmd4.Parameters.AddWithValue("@questiontype", DropDownList3.SelectedValue);
          cmd4.Parameters.AddWithValue("@answertype", DropDownList2.SelectedValue);
          cmd4.Parameters.AddWithValue("@optiontype", RadioButtonList1.SelectedValue);
       if(RadioButtonList1.SelectedIndex==1)
       {
           string str1 = HttpUtility.HtmlEncode(CKEditor1.Text);
           cmd4.Parameters.AddWithValue("@textquestion", str1);
       }
       else
       {
       if (FileUpload1.HasFile)
          {
              string str = FileUpload1.FileName;
              FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));
              string Image = "~/Upload/" + str.ToString();
              cmd4.Parameters.AddWithValue("@filequestion", Image);
          }
                   string str1 = HttpUtility.HtmlEncode(CKEditor1.Text);
       cmd4.Parameters.AddWithValue("@textquestion", str1);
       }
               cmd4.Parameters.AddWithValue("@course", DropDownList4.DataValueField);
              cmd4.Parameters.AddWithValue("@correctscore", TextBox1.Text);
              cmd4.Parameters.AddWithValue("@incorrectscore", TextBox2.Text);
              cmd4.Parameters.AddWithValue("@questiontime", TextBox3.Text);
              cmd4.Parameters.AddWithValue("@displayorder", TextBox4.Text);
              Label1.Text = "Data Added Successfully";
              TextBox1.Text = " ";
              TextBox2.Text = " ";
              TextBox3.Text = " ";
              TextBox4.Text = " ";
         cmd4.ExecuteNonQuery();
         con4.Close();
   }





我尝试过:



必须声明标量变量@filequestion。在编译下面的代码时遇到这个错误plz帮我解决了



What I have tried:

Must declare the scalar variable "@filequestion". am facing this error while compiling below code plz help me solve out

推荐答案

非常简单的调试会比你发布这个问题并且比你更快地得到答案它需要我写一个回复。



当你收到错误时,在错误之前放置一个断点,然后遍历代码,看看会发生什么。在这种情况下,您的sql语句表示您将插入值,并且值来自参数。但是在if语句中添加@filequestion参数。所以,如果if语句没有运行,你就不会传入@fielquestion参数,从而得到错误。



你需要在其中添加else条件做这样的事情:

Very simple debugging would get you an answer faster than it took you to post this question and faster than it takes me to write a response.

When you get an error, put a breakpoint before the error and then walk through the code and see what happens. In this case, your sql statement says you are going to insert values and the values are coming from parameters. But you add the @filequestion parameter in an if statement. So, if the if statement does not run, you are not passing in the @fielquestion parameter and therefore getting the error.

You need to add an else condition where you do something like this:
cmd4.Parameters.AddWithValue("@filequestion", DBNull.Value);


试试这个



使用默认值声明外部变量,并将值分配给 if condition 内的变量。将 cmd.parameters.add 置于条件之外,以便在任何一种情况下将所有必需参数添加到 sqlCommand 对象

try this

Declare the variables outside with a default value and assign the values to the variables inside the "if condition". Place the cmd.parameters.add outside the condition, so that in either cases all the required parameters are added to the sqlCommand Object
string str1 = "", Image = ""; // declare the variable 
           if (RadioButtonList1.SelectedIndex == 1)
           {
                 str1 = HttpUtility.HtmlEncode(CKEditor1.Text);
           }
           else
           {
               if (FileUpload1.HasFile)
               {
                   string str = FileUpload1.FileName;
                   FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));
                     Image = "~/Upload/" + str.ToString();

               }
           }
           cmd4.Parameters.AddWithValue("@textquestion", str1);
           cmd4.Parameters.AddWithValue("@filequestion", Image);


这篇关于必须声明标量变量& quot; @ filequestion&安培; QUOT; 。编译下面的代码plz时遇到这个错误帮我解决了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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