注册 登录
编程论坛 ASP.NET技术论坛

“/vote”应用程序中的服务器错误

redstar00 发布于 2006-09-01 15:26, 1472 次点击

请问:如下是什么错误?怎么改正?初.NET,望赐教
“/vote”应用程序中的服务器错误。

对象名 'voteMaster' 无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.SqlClient.SqlException: 对象名 'voteMaster' 无效。

源错误:


行 28: SqlCommand cmd=new SqlCommand("select voteTitle from voteMaster where voteId=" +this.voteId,con);
行 29:
行 30: string title=Convert.ToString(cmd.ExecuteScalar());
行 31: this.LblTitle.Text=title;
行 32: // 在此处放置用户代码以初始化页面

源文件: c:\inetpub\wwwroot\vote\vote.aspx.cs 行: 30

堆栈跟踪:


[SqlException: 对象名 'voteMaster' 无效。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteScalar()
vote.vote.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\vote\vote.aspx.cs:30
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


19 回复
#2
redstar002006-09-01 15:44
现在才觉得自学好难呀!身边连问的人也没有!上网请教高手,连个看帖子的人也没有!

苍天呀,大地呀!我现在除了感慨,还能干什么呢??????????
#3
redstar002006-09-01 15:46
我盼望哪位好心人,能在百无聊赖之余,帮着瞅瞅!!!!!
#4
mylover6242006-09-01 15:54
select voteTitle from voteMaster where voteId=" +this.voteId,con);
这说明在你当前是数据库中没有voteMaster 这个表.
#5
redstar002006-09-01 15:55
有这个表呀 没有问题
#6
mylover6242006-09-01 16:13

voteMaster 这你联接的数据库下面吗?
你把这句 select voteTitle from voteMaster where voteId=" +this.voteId 放到查询分析器里面执行一下,看有没有错误.

#7
redstar002006-09-01 16:30

没有错
select voteTitle from voteMaster

下面是数据库相关代码
create database vote

use vote
create table voteMaster
(
voteId int primary key,--编号
voteTitle varchar(100) not null,--项目
voteSum int default 0--总票数
)
insert into voteMaster values(1,'选举工会主席',0)
insert into voteMaster values(2,'对网站建设的意见',0)

create table voteDetails
(

voteId int foreign key references voteMaster(voteId),
voteDetailsId int not null,
voteItem varchar(20) not null,
voteNum int default 0
primary key(voteId,voteDetailsId)
)
insert into voteDetails values(1,1,'小王',0)
insert into voteDetails values(1,2,'小李',0)
insert into voteDetails values(1,3,'小张',0)

insert into voteDetails values(2,1,'非常好',0)
insert into voteDetails values(2,2,'好',0)
insert into voteDetails values(2,3,'一般',0)
insert into voteDetails values(2,4,'需要改进',0)

#8
mylover6242006-09-01 16:31
看一下你的数据库连接可以不?
#9
redstar002006-09-01 16:39


using System;
using System.Data.SqlClient;
namespace vote
{
/// <summary>
/// DB 的摘要说明。
/// </summary>
public class DB
{
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection createConnection()
{
SqlConnection con=new SqlConnection("server=.;database=vote;uid=sa;pwd=;");
return con;
}

}
}




using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace vote
{
/// <summary>
/// vote 的摘要说明。
/// </summary>
public class vote : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label LblTitle;
protected System.Web.UI.WebControls.Button BtnVote;
protected System.Web.UI.WebControls.Button BtnShow;
protected System.Web.UI.WebControls.RadioButtonList RBtnItems;
private string voteId="1";
private void Page_Load(object sender, System.EventArgs e)
{
//创建连接
SqlConnection con=DB.createConnection();
con.Open();
//查询选举的标题
SqlCommand cmd=new SqlCommand("select voteTitle from voteMaster where voteId=" +this.voteId,con);
string title=Convert.ToString(cmd.ExecuteScalar());
this.LblTitle.Text=title;

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

#10
mylover6242006-09-01 16:47
以下是引用redstar00在2006-9-1 16:39:40的发言:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace vote
{
/// <summary>
/// vote 的摘要说明。
/// </summary>
public class vote : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label LblTitle;
protected System.Web.UI.WebControls.Button BtnVote;
protected System.Web.UI.WebControls.Button BtnShow;
protected System.Web.UI.WebControls.RadioButtonList RBtnItems;
private string voteId="1";
private void Page_Load(object sender, System.EventArgs e)
{
//创建连接
SqlConnection con=DB.createConnection();
con.Open();
//查询选举的标题
SqlCommand cmd=new SqlCommand("select voteTitle from voteMaster where voteId='" + this.voteId + "'",con);
string title=Convert.ToString(cmd.ExecuteScalar());
this.LblTitle.Text=title;

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}




因为你的voteId是string 型,所以在用的时候得加上单引号.

#11
redstar002006-09-01 16:57
我按你的方法改了,可是还是存在相同的错误!!!
#12
mylover6242006-09-01 17:05
create table voteDetails
(
voteId int foreign key references voteMaster(voteId),



你将"private string voteId="1";"改成"private int voteId=1;"
然后:select voteTitle from voteMaster where voteId=" + voteId ;这个还是用你原来的
#13
redstar002006-09-01 17:10

还是先前的错误!!!!!!!

#14
redstar002006-09-01 17:31

斑竹:期待你的回应!!!!!!!!!!1

#15
mylover6242006-09-01 17:38
加个try....catch()...看看.
#16
redstar002006-09-01 17:40
能详细的说说的吗?因为我学.net 不是很久
#17
chenjin1452006-09-01 17:47
你拖控件去自動生成dataset 看看有沒數據

連接錯 或表名錯 要不就是你 連錯地方
#18
mylover6242006-09-01 17:58
[CODE] private void Page_Load(object sender, System.EventArgs e)
{
//创建连接
SqlConnection con=DB.createConnection();
con.Open();
//查询选举的标题
SqlCommand cmd=new SqlCommand("select voteTitle from voteMaster where voteId=" +this.voteId,con);
string title=Convert.ToString(cmd.ExecuteScalar());
this.LblTitle.Text=title;

}[/CODE]
改成

[CODE]private void Page_Load(object sender, System.EventArgs e)
{
//创建连接
string title = "";
try
{
SqlConnection con=DB.createConnection();
con.Open();
//查询选举的标题
SqlCommand cmd=new SqlCommand("select voteTitle from voteMaster where voteId=" +this.voteId,con);
title = Convert.ToString(cmd.ExecuteScalar());
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
}
this.LblTitle.Text=title;

}[/CODE]
#19
沙僧2006-09-02 01:03
没想到有人跟我一样可怜:

private int voteId = 1;
private void Page_Load(object sender, System.EventArgs e)
{
//创建连接
SqlConnection con=DB.createConnection();
con.Open();
//查询选举的标题
SqlCommand cmd=new SqlCommand("select voteTitle from voteMaster where voteId=" +voteId+ "",con);
string title=Convert.ToString(cmd.ExecuteScalar());
this.LblTitle.Text=title;

}

如果不成功,继续哼一声。
#20
sj8219262007-06-13 15:24
回复:(redstar00)“/vote”应用程序中的服务器错误...
<FORM id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
</FORM>
多了两遍这样的代码,删除就可以了
1