我一直收到错误System.InvalidCastException:指定的强制转换无效.在运行时.数据库中的RewardJoinType可以为null.
这是强制转换失败的代码行:
c.rewardJoinType = (RewardJoinType)reader.GetInt16();
Run Code Online (Sandbox Code Playgroud)
'reader.GetInt16()'引发了一个'System.InvalidCastException'类型的异常short {System.InvalidCastException}
在课堂上,我有以下几行代码:
private RewardJoinType? rewardJoinType;
Run Code Online (Sandbox Code Playgroud)
......其他一些代码
c.rewardJoinType = (RewardJoinType?)reader.GetInt16();
Run Code Online (Sandbox Code Playgroud)
......其他一些代码
conn.AddParam("@rewardJoinType", (int?)rewardJoinType);
Run Code Online (Sandbox Code Playgroud)
......其他一些代码
public RewardJoinType? RewardJoinType
{
get { return rewardJoinType; }
set { rewardJoinType = value; }
}
Run Code Online (Sandbox Code Playgroud)
这是枚举本身
public enum RewardJoinType
{
Auto,
Manual
}
Run Code Online (Sandbox Code Playgroud)
是因为默认情况下枚举是Int32,即使我已经可以为空,它也无法转换为null Int16?
我们在读者中处理了Int16的DBNull:
public short GetInt16()
{
columnIndex++;
return reader.IsDBNull(columnIndex) ? (short)0 : reader.GetInt16(columnIndex);
}
Run Code Online (Sandbox Code Playgroud) 必须有一种更简单的方法来完成我在这里想到的事情:
int lastDayInList = ddlBirthDay.Items.IndexOf(ddlBirthDay.Items[ddlBirthDay.Items.Count -1]);
Run Code Online (Sandbox Code Playgroud) 有没有一种方法可以告诉我(除了在SVN日志中...因为它只显示我有人提交)最近更新了以便我知道他们已经下载了我的代码?我正在专门研究更新(不是在SVN日志中可以看到的提交).
我假设如果total为null,这将会爆炸(我不记得小数是否在开头被初始化为零或零)
public int SomePropertyName
{
get { return Convert.ToInt32(Decimal.Round(total)); }
}
Run Code Online (Sandbox Code Playgroud)
那么我应该检查null还是> 0?
我觉得这是一堆你知道我的意思.它的工作原理,但我觉得我在页面生命周期(加载和回发)方面做得太过分了,甚至我在每个if语句中都有冗余.
这是怎么回事:
因此,我也在用户声明点(提交)之后立即调用此方法,该点将从下一次的总数中删除这些点.因此,基于其帐户中的总分,我需要在页面从上次提交刷新后启用/禁用这些单选按钮
private void SetPointsOptions()
{
int totalPoints = customer.TotalPoints;
rbn200Points.Text = "200 pts";
rbn250Points.Text = "250 pts";
rbn400Points.Text = "400 pts";
rbn500Points.Text = "500 pts";
rbn600Points.Text = "600 pts";
// clear state of radio buttons & disable submit
if (totalPoints < 200)
{
rbn200Points.Enabled = false;
rbn250Points.Enabled = false;
rbn400Points.Enabled = false;
rbn500Points.Enabled = false;
rbn600Points.Enabled = false;
rbn200Points.Checked = false;
rbn250Points.Checked = false;
rbn400Points.Checked = false;
rbn500Points.Checked = false;
rbn600Points.Checked = false;
btnClaimRewardPoints.Enabled = false; …Run Code Online (Sandbox Code Playgroud) 我的代码中有以下方法.我的老板问"你为什么要使用字典",你可以使用数组,效率更高:
public static Dictionary<string, string> GetListOfMonths()
{
Dictionary<string, string> months = new Dictionary<string, string>();
months.Add("1", "Jan");
months.Add("2", "Feb");
months.Add("3", "Mar");
months.Add("4", "Apr");
months.Add("5", "May");
months.Add("6", "Jun");
months.Add("7", "Jul");
months.Add("8", "Aug");
months.Add("9", "Sep");
months.Add("10", "Oct");
months.Add("11", "Nov");
months.Add("12", "Dec");
return months;
}
Run Code Online (Sandbox Code Playgroud)
我使用它来代码重用.所以我可以绑定到应用程序各个部分的一些月下拉菜单.
我在这里找到了一个有趣的讨论
引用:
DataSet是用于编码数据层的业余解决方案......停止使用它们并学习代码!:)
您对DataSet的看法是什么?只是简化自定义类并与之合作?还有什么其他选择?
在我的.aspx中我定义了以下javascript变量:
var action = <%=ProdView %>
Run Code Online (Sandbox Code Playgroud)
在代码隐藏中,它返回一个自定义枚举值:
protected ProductView ProdView { get; private set; }
Run Code Online (Sandbox Code Playgroud)
我想这会自动转换为javascript中的字符串?看起来没有,因为我得到运行时错误"Item is not defined",其中Item是值ProdView.Item.最终我希望动作的值为"Item"作为值.
这是Enum:
public enum ProductView
{
Product,
Item
}
Run Code Online (Sandbox Code Playgroud) 我在.aspx的代码隐藏中有以下属性:
protected string CurrentProductView
{
get
{
string viewName = string.Empty;
viewName = Enum.GetName(typeof(ProductView), currentProdView);
return viewName;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的.aspx中我有一些试图引用这个字符串的Javascript:
$(document).ready(function ()
{
var action = <%=CurrentProductView %>
$("#recommendations").load("recommendationsHandler.ashx?action=" + action + "item&csid=" + csid + "&productID=" + productID, function()
{
$("#recommendationsView").show();
});
});
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,我得到"项目未定义".
当我调试这个时,我肯定会看到一个字符串返回viewName.那么为什么它会抱怨如果一个字符串回来?!?!
我在JS中创建了这个实用工具方法:
function IsAuthenticated(userID)
{
var isAuthed = false;
if (userID.length == 0)
return false;
// more logic
if(SomeLogic)
isAuthed = true;
return isAuthed;
}
Run Code Online (Sandbox Code Playgroud)
当我运行这样的东西时,我得到一个对象而不是键入bool:
if(IsAuthenticated)
//code here
Run Code Online (Sandbox Code Playgroud)
我想我需要把它变成一个布尔?
c# ×6
asp.net ×3
javascript ×3
control-flow ×1
dataset ×1
jquery ×1
optimization ×1
svn ×1
tortoisesvn ×1