2016-02-19 64 views
-2
protected void btnAdd_Click(object sender, EventArgs e) 
{ 
    if (!string.IsNullOrWhiteSpace(Request.QueryString["id"])) 
    {  
     string kundeID = "-1"; 
     int id = Convert.ToInt32(Request.QueryString["id"]); 
     int totalsum = Convert.ToInt32(ddlAmount.SelectedValue); 

     Handlevogn handlevogn = new Handlevogn 
     { 
      TotalSum = totalsum, 
      KundeID = kundeID, 
      Dato = DateTime.Now, 
      ErIHandlevogn = true, 
      ProduktID = id 
     }; 

     HandlevognModell modell = new HandlevognModell(); 
     lblResult.Text = modell.InsertHandlevogn(handlevogn); 
    } 

不断收到错误无法隐式转换类型“字符串”到“短”

无法隐式转换typre“字符串”到“短”

本地变量kundeID。

回答

0

该警告相当详细。你应该将kundeID解析为short(或者将KundeID改为string)。

KundeID = short.Parse(kundeID); 
3

最简单的办法是改变你以前在这里做你的kundelIDshort

short kundelID = -1; 

那么你的隐式转换:

KundelID = kundelID; //this is called implicit because you implicitly tell kundelID, which is a short to be changed to KundelID, which is likely a short 

将同类型short的。

如果您需要kundelID作为string出于某种原因,请在使用前将其正确地转换为KundelID

KundelID = Convert.ToInt16(kundelID); 

然后,它也是正确的。

0

您可以使用函数:

Int16.TryParse(string value, out number); 

谁将会返回一个布尔值,真要是如果不是函数可以施放它和虚假。

你可以使用它的一个如果:

short id; 
if(Int16.TryParse(Request.QueryString["id"], out id)) 
{ 
    ProduktID = id; 
} 
相关问题