int id = convert.toint32(session [" id"])的正确输入字符串是什么? [英] What is the correct input string for int id = convert.toint32(session["id"]);

查看:64
本文介绍了int id = convert.toint32(session [" id"])的正确输入字符串是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
        {
                if(!IsPostBack)
            {
                if(Session["Id"]!=null)
                {
                    int id = Convert.ToInt32(Session["Id"]);
                    GetStudentDataById(id);
                }
            }





我的尝试:



我尝试过使用字符串,但仍显示输入字符串格式不正确



What I have tried:

I have tried with the string but still it shows that "The Input string was not in a correct form"

推荐答案

它必须可以转换为int,我认为这是相当不言自明的? 123很好你好世界很糟糕。如果你告诉我们会话[Id]我们可以解释为什么它不起作用。



无论如何,转换时使用int.TryParse字符串到整数,因为它可以让你优雅地处理错误。 Google用于语法用法。
It has to be convertible to an int, I think that's fairly self-explanatory? "123" is good "Hello world" is bad. If you told us what was in Session["Id"] we might be able to explain why it isn't working.

Regardless, use int.TryParse when converting strings to integers as it lets you gracefully handle errors. Google for syntax usage.


始终使用 Int32.TryParse [ ^ ]将字符串转换为整数



Always use Int32.TryParse [^] to cast a string to Integer

protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               if (Session["Id"] != null)
               {
                   int id;
                   if (int.TryParse(Session["Id"].ToString(), out id))
                       GetStudentDataById(id); // id is a valid number
                   else
                   {
                        // id is not a number.
                       // your code to validate, saying the id is invalid
                   }
               }
           }


       }


请试用此代码,





Please try this code,


protected void Page_Load(object sender, EventArgs e)
        {
                if(!IsPostBack)
            {
                if(Session["Id"]!=null)
                {
                    int id = Convert.ToInt32(Session["Id"].ToString());
                    GetStudentDataById(id);
                }
            }


这篇关于int id = convert.toint32(session [" id"])的正确输入字符串是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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