Slash

习惯在追逐的过程中不断去完善自己;当你不再去追逐,你自我完善的脚步也就停滞下来了。

导航

类型转换问题费了我半天光阴,汗.......

    今天做用户登录页面ilogon.aspx可真是让我吃尽苦头,花了整整八个小时,单步,测试等各种方法用尽了最后才总算告一段落,究其原因竟然是类型转换问题,冒汗不已。可能是由于实践经验不足导致,想起以前看书都是觉得想类型转换等属于小问题而不曾放在心上,一直也未曾接触什么项目开发经验,这次算是让我认识到什么叫"不可小觑"了,特写此文以示警戒自己。
    登录页面是通过<iframe src='ilogon.aspx'></iframe>,验证通过后将直接进入ilogin.aspx,使用Session保存用户状态,以下为正确登录代码:
 1        private void btnSubmit_Click(object sender, System.EventArgs e)
 2        {    
 3            if((Page.IsValid&&Session["ValidateCode"]!=null)&&txtValidatecode.Text.Trim().ToString()==Session["ValidateCode"].ToString())
 4            {
 5                //取出临时购物车CartID
 6                EasybuyExcuData tempcart=new EasybuyExcuData();
 7                String tempcartid=tempcart.GetCartID();
 8                
 9                //取出用户名内容
10                string username=txtName.Text.Trim().ToString();
11                //取出密码内容
12                string pwd=txtPwd.Text.Trim().ToString();
13               // EasybuyExcuData edata=new EasybuyExcuData();
14                int userid=tempcart.UserLogin(username,pwd);
15                
16                if(userid!=0)
17                {
18                    Session["userid"]=userid;
19                    SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
20                    SqlCommand com=new SqlCommand("select count(*) from ShoppingCart where CartID= @userid",con);
21                    com.Parameters.Add("@userid",SqlDbType.NVarChar,50).Value =userid.ToString();
22                    
23                    con.Open();
24                    int count=(Int32)com.ExecuteScalar();
25                    con.Close();
26                    if(count>0)
27                    {
28                        tempcart.TransplantShoppingCart(tempcartid.ToString(),userid.ToString());
29                    }

30                    //Response.Redirect("ilogin.aspx");
31                    Response.Redirect("testiframe.htm");
32                }

33                else
34                {
35                    lbMessage.Text="登录失败!请确认用户名或密码无误!";
36                    //lbMessage
37                    //Response.Redirect("Ilogonpage.aspx");response.redirect("welcome.aspx")
38                }

39                    
40            }

41            else
42            {
43                lbMessage.Text="验证码不匹配,请检查后重新输入!";
44                lbMessage.Visible=true;        
45
46            }

47            
48        }
     

posted on 2006-03-27 19:06  Slash  阅读(323)  评论(1编辑  收藏  举报