Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
      }
}
Posted
Updated 21-Jul-10 6:58am
v2
Comments
Sandeep Mewara 21-Jul-10 14:01pm    
Post the Stored Procedure here! We need to see the SP and queries inside it.
mudassir 1988 22-Jul-10 2:21am    
delimiter$$

DROP PROCEDURE IF EXISTS `mysql`.`GetActivePoll` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetActivePoll`()
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 $$


delimiter ;
I think the error accour due to Active how can i correct it
plz help me

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:
mudassir 1988 wrote:
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


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:
C#
// 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
}
 
Share this answer
 
v2
Comments
mudassir 1988 22-Jul-10 2:19am    
Yes u r right my query are disable to select 'Answer' .my query r as follows
delimiter$$

DROP PROCEDURE IF EXISTS `mysql`.`GetActivePoll` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetActivePoll`()
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 $$


delimiter ;
I think the error accour due to Active how can i correct it
plz help me
Thanks
Sandeep Mewara 22-Jul-10 2:54am    
I updated my answer with your issue and resolution. Have a look.
Sandeep Mewara 22-Jul-10 4:47am    
What happened, does my answer resolves your issue? Expecting an update from you after so much...
mudassir 1988 23-Jul-10 6:31am    
Hi when i debug my code than i saw ,at line ,foreach (DataRow dr in ds.Tables[1].Rows).it cant come at data row and cant give exception ,and goto the line display poll and display the poll but without option what i do.
plz help me
I think your Stored procedure GetActivePoll doesn't contain the resultset column Answer. Check the sp & tell us.
 
Share this answer
 
Comments
mudassir 1988 21-Jul-10 13:58pm    
it give resultset column Answer.what i do
mudassir 1988 22-Jul-10 2:22am    
Yes u r right my query are disable to select 'Answer' .my query r as follows
delimiter$$

DROP PROCEDURE IF EXISTS `mysql`.`GetActivePoll` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetActivePoll`()
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 $$


delimiter ;
I think the error accour due to Active how can i correct it
plz help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900