这是cs代码using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Xml.Linq;
public partial class zizhutingcheaspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string strConn;
SqlDataAdapter objDataAdapter;
strConn = "server=localhost;database=intelliCampus;uid=sa;pwd=123"; //不区分大小写,ok //值为true(同sspi),OK
SqlConnection myConn = new SqlConnection(strConn);
myConn.Open();
SqlCommand cmd = new SqlCommand("select * from tb_sensor_parkingPlace", myConn);
objDataAdapter = new System.Data.SqlClient.SqlDataAdapter("SELECT [status] FROM [tb_sensor_parkingPlace]", myConn);
DataTable dt = new DataTable();
objDataAdapter.Fill(dt);
int[] a = new int[27];
for (int i = 0; i <= 27; i++)//这有时也提示越界错误
{
a[i] = int.Parse(dt.Rows[i].ToString());//这提示formatexeceptionhe
错误 功能是从数据库的一个表中取出某一列赋给一个数组}
for (int j = 0; j < 27; j++)
{
int n = j + 13; if (a[j] == 0)
{
FindControl("apDiv" + n.ToString()).Visible = false;//隐藏 }
else
{
FindControl("apDiv" + n.ToString()).Visible = true;//显示
}
}
myConn.Close();//关闭数据库的连接
myConn = null; }
}
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Xml.Linq;
public partial class zizhutingcheaspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string strConn;
SqlDataAdapter objDataAdapter;
strConn = "server=localhost;database=intelliCampus;uid=sa;pwd=123"; //不区分大小写,ok //值为true(同sspi),OK
SqlConnection myConn = new SqlConnection(strConn);
myConn.Open();
SqlCommand cmd = new SqlCommand("select * from tb_sensor_parkingPlace", myConn);
objDataAdapter = new System.Data.SqlClient.SqlDataAdapter("SELECT [status] FROM [tb_sensor_parkingPlace]", myConn);
DataTable dt = new DataTable();
objDataAdapter.Fill(dt);
int[] a = new int[27];
for (int i = 0; i <= 27; i++)//这有时也提示越界错误
{
a[i] = int.Parse(dt.Rows[i].ToString());//这提示formatexeceptionhe
错误 功能是从数据库的一个表中取出某一列赋给一个数组}
for (int j = 0; j < 27; j++)
{
int n = j + 13; if (a[j] == 0)
{
FindControl("apDiv" + n.ToString()).Visible = false;//隐藏 }
else
{
FindControl("apDiv" + n.ToString()).Visible = true;//显示
}
}
myConn.Close();//关闭数据库的连接
myConn = null; }
}
红色部分可能是空串或者不能转化为数字的字符
for (int i = 0; i <= 27; i++)//这有时也提示越界错误你都不知道DataTable里面总共有多少条记录,如果比28条少的话,肯定会越界了
int[] a = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
for (int i = 0; i <= 27; i++)//这有时也提示越界错误
{
a[i] = int.Parse(dt.Rows[i].ToString());//这提示formatexeceptionhe
for (int i = 0; i <= 27; i++)//这有时也提示越界错误
{
a[i] = int.Parse(dt.Rows[i].ToString());//这提示formatexeceptionhe
错误 功能是从数据库的一个表中取出某一列赋给一个数组}你是有声明一个27行的数组,你是,别忘了程序是0开始索引。
也就说,是有27行,但程序只读到26.改成这样吧 for (int i = 0; i < 27; i++)//去掉等号
{
a[i] = int.Parse(dt.Rows[i]);}
有27条数据 你取出来 这个应该是可变的a[i] = int.Parse(dt.Rows[i]["列名"].ToString());
你断点看你取出来的数据时什么格式 能进行转换按吗?
错误 功能是从数据库的一个表中取出某一列赋给一个数组}指定行没有指定列啊 dt.Rows[i]["列名"] 或dt.Rows[i]["索引"]