如何从表中读取值 [英] how to read a value from a table
本文介绍了如何从表中读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编写的代码是这样的,并且在系统中发生的System.IndexOutOfRangeException未在用户代码和附加信息中处理:Percentage1
code written is like this and "System.IndexOutOfRangeException" occurred in system.dll was not handled in user code and additional information:Percentage1
DB obj = new DB();
if (obj.IsInvoiceExists(oTestModel.InvoiceNo))
{
//float Percent75;
string message;
//oTestModel.Col25 = (Constants.PERCENT25 / Constants.TOTATL100) * oTestModel.Amount;
//oTestModel.Col75 = (Constants.PERCENT75 / Constants.TOTATL100) * oTestModel.Amount;
//Percent75 = oTestModel.Col75;
//oTestModel.Result = (Percent75) - (oTestModel.PayRoll + oTestModel.Expense + oTestModel.PerDiem + oTestModel.Medical + oTestModel.DiscretionaryBonus);
string connstring = "Data Source=.;Initial Catalog=Accounts;Integrated Security=True";
string query = "select *from Employee";
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(query, conn);
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
int EmpId = Convert.ToInt32(read["EmpId"]);
string EmpName = (read["EmpName"].ToString());
string Designation = (read["Designation"].ToString());
oTestModel.Col25 = Convert.ToSingle(read["Perccentage1"]) / 100 * oTestModel.Amount;
oTestModel.Col75 = Convert.ToSingle(read["Percentage2"]) / 100 * oTestModel.Amount;
string Location = (read["Location"].ToString());
oTestModel.Result = (oTestModel.Col75) - (oTestModel.PayRoll + oTestModel.Expense + oTestModel.PerDiem + oTestModel.Medical + oTestModel.DiscretionaryBonus);
}
read.Close();
obj.SaveData(oTestModel);
message = oTestModel.InvoiceNo + " invoice inserted successfully";
//TempData["ViewBag.Message"] = "Successfully registered";
Response.Write("<script>alert('" + message + "');</script>");
conn.Close();
}
推荐答案
您已写过
You have written
read["Perccentage1"]
应该是
It should be
read["Percentage1"]
一个简单的拼写错误。
A simple typo mistake.
这篇关于如何从表中读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文