获取Exception将数据类型nvarchar转换为numeric [英] getting Exception as converting data type nvarchar to numeric

查看:122
本文介绍了获取Exception将数据类型nvarchar转换为numeric的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我在第da.Fill(dt)行获得了Exception;我正在开发在线考试



//i am getting Exception at line da.Fill(dt); i am developing online Exam

public partial class WebForm9 : System.Web.UI.Page
    {
       
        SqlCommand cmd = new SqlCommand();
        SqlCommand cmd1 = new SqlCommand();
       DataTable dt = new DataTable();
        DataRow drr;
        public string ans_tea;
        public string ans_stu;
        SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=swatidb;Integrated Security=True;Pooling=False");

        static int i = 0;
        
       
       
           public void Page_Load(object sender, EventArgs e)
        {
       
            con.Open();
            string s1 = "select student_id from Student where user_name='" + Session["sid"] + "'";
            SqlCommand cmd = new SqlCommand(s1, con);

            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Label4.Text = dr[0].ToString();
                dr.Close();
            }
            String tea_id = Convert.ToString(TextBox2.Text);
            string commandText = "SELECT * from Question WHERE Teacher_id=@tea_id";
            using (SqlConnection connection = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=swatidb;Integrated Security=True;Pooling=False"))
            {
                using (SqlCommand command = new SqlCommand(commandText, con))
                {
                    command.Parameters.AddWithValue("@tea_id", tea_id);
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    drr = dt.Rows[i];
                    
                }
            }

             public void Button2_Click(object sender, System.EventArgs e)
        {
            
          //  Label2.Text = Convert.ToString(dr[2]);
           
            //Label3.Text = Convert.ToString(dr[3]);
            //TextBox3.Text = Convert.ToString(dr[2]);
            //TextBox4.Text = Convert.ToString(dr[3]);
                                 if (i == (dt.Rows.Count - 1))
            {
                Response.Write("Last record !");
            }
            else
            {
                //i = 0;
               i++;
             
            }
                                // drr = dt.Rows[i];
                                 Label3.Text = Convert.ToString(drr[0]);
                                 Label2.Text = Convert.ToString(drr[2]);
 
                      //Label2.Text = Convert.ToString(dr[2]);
                                // i = 0;
                                 con.Close();
       }

       }

推荐答案

根据我在帖子上的更新解决方案确认错误必须声明标量变量@tea_id [< a href =http://www.codeproject.com/Questions/748024/getting-error-as-Must-declare-the-scalar-variabletarget =_ blanktitle =New Window> ^ ]



问题出在
As per my updated solution on your post getting error as Must declare the scalar variable "@tea_id"[^]

The problem is with
String tea_id = Convert.ToString(TextBox2.Text);



首先 TextBox2.Text 已经是String,因此无需转换它。



其次看起来Teacher_id可能是整数或长整数。因为您已将tea_id声明为字符串,所以


Firstly TextBox2.Text is already a String, so no need to convert it.

Secondly it would appear that Teacher_id is probably an integer or a long. Because you have declared tea_id as a string, the line

command.Parameters.AddWithValue("@tea_id", tea_id);

将导致SQL命令

SELECT * from Question WHERE Teacher_id='9999'

(其中9999是TextBox2中的任何内容)。注意'9999'周围的单引号 - 就SQL而言是一个varchar。



如果你做了我建议的修正那么你会得到一个SQL命令

(where 9999 is whatever was in TextBox2). Note the single quotes around '9999' - i.e. a varchar as far as SQL is concerned.

If you make the correction I have suggested then you will get a SQL command of

SELECT * from Question WHERE Teacher_id=9999




i.e.

long tea_id=long.Parse(TextBox2.Text);





注意: TryParse 方法在接受用户输入时通常更好,我只使用 Parse 这里简洁。



Note: the TryParse method is usually better when accepting input from a user, I've only used Parse for brevity here.


这篇关于获取Exception将数据类型nvarchar转换为numeric的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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