以下代码抛出编译时错误,如
无法将'string'类型转换为'int'
string name = Session["name1"].ToString();
int i = (int)name;
Run Code Online (Sandbox Code Playgroud)
而下面的代码编译成功并执行:
string name = Session["name1"].ToString();
int i = Convert.ToInt32(name);
Run Code Online (Sandbox Code Playgroud)
我想知道:
为什么第一个代码会产生编译时错误?
2个代码片段之间的区别是什么?
我正在尝试向DataRowC#中的现有列添加列.之后,该列将填充我的数据库中的单个值.
DataRow dr已存在且列"COLNAME"也存在.
comTBP是我的SqlCommand.
dr["COLNAME"] = Convert.ToInt32(comTBP.ExecuteScalar());
Run Code Online (Sandbox Code Playgroud)
如果我的数据库中有值,并且ExecuteScalar()可以获得该值,则一切正常.如果我在我的开发服务器(本地)上测试此代码,如果ExecuteScalar()返回null或DBNull并且我的新列的值为0,它也可以工作.但是如果我将代码部署到生产服务器,则会出现问题.如果我做同样的事情,使用相同的数据库,它会抛出一个异常,并显示一条消息,它无法将DBNull转换为Int32.
我的问题是为什么这个错误出现在生产服务器上而不是我的本地开发服务器上?
int topID = 0;
string TopIDQuery = "Select TopID from tbl_Organisation where OrganisationID=@OrgID";
paramet[0] = new MySqlParameter("@OrgID", MySqlDbType.Int32);
paramet[0].Value = OrgID;
reader = server.ExecuteReader(CommandType.Text, TopIDQuery, paramet);
while (reader.Read())
{
topID = Convert.ToInt32(reader["TopID"]);
}
reader.Close();
Run Code Online (Sandbox Code Playgroud)
我正在topID从表中读取,当TopID为null时,我想保留topID为0,但由于它为null,因此抛出错误,如何在topIDnull 时处理此错误
为什么会这样呢?
int collectionCharge = (int)cmdCheck.ExecuteScalar();
Run Code Online (Sandbox Code Playgroud)
但这会产生异常
double collectionCharge = (double)cmdCheck.ExecuteScalar();
Run Code Online (Sandbox Code Playgroud)
System.InvalidCastException:指定的强制转换无效.
为什么它不会有效?
编辑
我试图简单地从查询中返回一个结果,获得一些运费的价格.所以我不能把它变成一个int因为它必须有小数,因此试图转换为double.我还在学习asp.net,所以如果有更好的方法来实现这一点,请指出我正确的方向:)
使用SQL 编辑2完整代码...
using (SqlCommand cmdCheck = new SqlCommand("SELECT FREIGHT_PRICE FROM FREIGHT_QUOTER_BELNL_NEW WHERE CUSTOMER_NO = @CUSTOMER_NO AND COUNTRY = @COUNTRY AND ROUND(WEIGHT_FROM,0) < @WEIGHT AND ROUND(WEIGHT_TO,0) >= @WEIGHT AND SHIPVIA = '48';", connection))
{
double collectionCharge = (double)cmdCheck.ExecuteScalar();
FreightAmount = collectionCharge;
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码可能返回没有找到结果 - null
String result
string searchUby = "SELECT text FROM rooms WHERE year=@year AND event=@eventAND text=@text AND z is NULL";
SqlCommand sqlcom = new SqlCommand(searchUby, conn);
sqlcom.Parameters.AddWithValue("@event",event.Text);
sqlcom.Parameters.AddWithValue("@text", cb_room.SelectedItem);
sqlcom.Parameters.AddWithValue("@year",klientClass.Year());
conn.Open();
result = sqlcom.ExecuteScalar().ToString(); // on this line ex occurs
conn.Close();
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
NullReferenceException: Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
愿有人帮我解决这个问题吗?
在我的数据库中,我有6列,所有列均允许空值。我想制作一个具有更新功能的简单表格。
产品,SNO,BTCH到期,数量,费率和金额。
我有一个例外:
无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象
这是我的代码:
int a = dataGridView1.CurrentCell.RowIndex;
try
{
using (AmdDataSet.Test_ibrahimDataTable table = new AmdDataSet.Test_ibrahimDataTable())
{
int b = a+1;
using (AmdDataSetTableAdapters.Test_ibrahimTableAdapter adp = new AmdDataSetTableAdapters.Test_ibrahimTableAdapter())
{
adp.GetDataBySNO(a);
AmdDataSet.Test_ibrahimRow erow;
erow = table.NewTest_ibrahimRow();
lblSNO.Text = erow.SNO.ToString();
txtProduct.Text= erow.PRODUCTS;
txtExpiry.Text = erow.EXPIRYDATE.ToShortDateString();
txtBatchNo.Text = erow.BATCHNO.Trim().ToString();
}
}
}
catch (Exception ex)
{
lbleror.Text = ex.Message;
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
UploadImageControl1.BinaryData =ServiceInfoDt["SERVICE_LOGO"]!=null?(byte []) ServiceInfoDt["SERVICE_LOGO"]:null;
Run Code Online (Sandbox Code Playgroud)
BinaryData 是一个字节数组 byte[]
买我收到这个错误:
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
Run Code Online (Sandbox Code Playgroud) 我正在尝试将记录提取到listarray中,如下所示:
List<Car> lst = new List<Car>();
string str = "select * from Inventory";
using(SqlCommand cmd = new SqlCommand(str,this.sqlcon))
{
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
lst.Add(new Car
{
CarId = (int)rdr["CarId"],
Make = (string)(rdr["Make"] ?? ""),
Color= (string)(rdr["Color"] ?? ""),
PetName = (string)(rdr["PetName"] ?? "")
});
}
rdr.Close();
}
Run Code Online (Sandbox Code Playgroud)
Make,color和petname可能具有空值,因此我使用了??运算符.我收到以下错误
无法将system.dbnull类型的对象强制转换为'system.string'.
在这种情况下检查null的正确方法是什么?