mysql中的asp.net错误 [英] asp.net error in mysql
问题描述
我遇到了错误
System.ArgumentException:列"Answer"不属于表Table
I got error
System.ArgumentException: Column ''Answer'' does not belong to table Table
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data.Odbc;
partial class Poll: System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
//lblPollQuestion.Text = "Answer";
DisplayPoll();
}
}
private void DisplayPoll()
{
try
{
DataSet ds = GetActivePoll();
// ArrayList listCols = new ArrayList();
// Displays the poll
lblPollQuestion.Text = (ds.Tables[0].Rows[0]["Question"]).ToString();
int i = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
//rdoPollOptionList.Text = dr[0].ToString(); //gives correct value
//rdoPollOptionList.Text = (ds.Tables[0].Columns[0]).ToString(); // gives "PNR"
//rdoPollOptionList.Text =(dr["Answer"]).ToString(); //Gives error, why?
//rdoPollOptionList.Items[i].Value = dr["PK_OptionId"].ToString();
//foreach ( Column in dtTable.Columns)
rdoPollOptionList.Items.Add(dr["Answer"].ToString());
rdoPollOptionList.Items[i].Value = dr["PK_OptionId"].ToString();
rdoPollOptionList.SelectedIndex =0;
i++;
}
} catch (Exception ex)
{
// throw ex;
}
}
private DataSet GetActivePoll()
{
try
{
string strConnString = (System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"]).ToString();
//SqlConnection sqlConn = new SqlConnection(strConnString);
OdbcConnection MyConnection = new OdbcConnection(strConnString );
MyConnection.Open();
//SqlClient.SqlConnection sqlConn = new SqlClient.SqlConnection();
//sqlConn.ConnectionString = ("Data Source=C:InetpubwwwrootPollsPollsApp_Data.mdf");
// Opens the connection
//sqlConn.Open();
OdbcCommand sqlCmd = new OdbcCommand();
sqlCmd.CommandText = "{call GetActivePoll}";
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Connection = MyConnection ;
// Gets the dataset from the sp
DataSet ds = new DataSet();
OdbcDataAdapter da = new OdbcDataAdapter(sqlCmd);
// Execute stored procedure
da.Fill(ds);
// Close connection
MyConnection.Close();
return ds;
} catch (Exception ex)
{
throw ex;
}
}
protected void btnVote_Click( object sender , System.EventArgs e)
{
if (Response.Cookies["Voted"]!= null)
{
Response.Cookies["Voted"].Value = "Voted";
Response.Cookies["Voted"].Expires = DateTime.Now.AddDays(1);
lblError.Visible = false;
// Checks if the user can still vote by using cookie
RecordVote();
} else
{
lblError.Visible = true;
}
}
private void RecordVote()
{
string strConnString= System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
// SqlConnection sqlConn = new SqlConnection(strConnString);
OdbcConnection MyConnection = new OdbcConnection(strConnString );
MyConnection.Open();
//SqlClient.SqlConnection sqlConn = new SqlClient.SqlConnection();
//sqlConn.ConnectionString = "Data Source=Ser
OdbcCommand sqlCmd = new OdbcCommand();
sqlCmd.CommandText = "{call IncreamentVotes(?)}";
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Connection = MyConnection;
// Creation parameters
OdbcParameter sqlParamQuestion = new OdbcParameter("@i_OptionId",SqlDbType.Int);
sqlParamQuestion.Value = rdoPollOptionList.SelectedValue;
sqlCmd.Parameters.Add(sqlParamQuestion);
// Execute stored procedure
sqlCmd.ExecuteNonQuery();
// Close connection
MyConnection.Close();
}
}
推荐答案
1.您在昨天发布了相同的内容 [ 根据正确的原因更正此问题.
更新SM:
1. You posted the same thing yesterday[^]
2. Though we asked you about your table structure/query thats fetching the result, you didnt replied that. Instead repost the same thing.
It''s clear from the error that either a column named ''Answer'' does not exists in the table you are accessing OR ''Answer'' was not selected in your query.
Correct the issue, based on which ever reasons is true.
UPDATE SM:
BEGIN SELECT PK_PollId,来自pol_question的问题,其中Active + 0 ="1"; SELECT PK_OptionId,答案,从民意测验的投票中FK_PollId IN(从pol_question中选择PK_PollID主动+0 =``1''); END
BEGIN SELECT PK_PollId, Question FROM pol_question WHERE Active+0 = ''1'' ; SELECT PK_OptionId, Answer, Votes FROM polloptions WHERE FK_PollId IN (SELECT PK_PollID FROM pol_question WHERE Active+0 = ''1''); END
干得好!您的存储过程中有两个select语句.因此,返回的数据集将有两个表,第二个表将具有答案"列.查看您的代码,您正在尝试访问第一个表中的答案"列.
尝试像这样访问:
Here you go! You have two select statements in your Stored Procedure. Thus the dataset returned will have two tables and the second table would have this ''Answer'' column. Look at your code, you are trying to access the ''Answer'' column in first table.
Try accessing like this:
// Changed the table index from '0' to '1' here.
foreach (DataRow dr in ds.Tables[1].Rows)
{
rdoPollOptionList.Items.Add(dr["Answer"].ToString());
// other stuffs
}
我认为您的存储过程 GetActivePoll 不包含结果集列答案.检查sp&告诉我们.
I think your Stored procedure GetActivePoll doesn''t contain the resultset column Answer. Check the sp & tell us.
这篇关于mysql中的asp.net错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!