2

I have a function that calls a stored procedure. But it throws an exception some thing like this :

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Invalid column name 'quantity'.

Invalid column name 'quantity'.
Invalid column name 'location'.
Invalid column name 'quantity'.
Invalid column name 'quantity'.
Invalid column name 'quantity'. ...

I don't understand why this exception is thrown because the stored procedure works fine in MSSMS.

Here is my code for calling the stored procedure:

public DataSet getDataTable_sp(string sp_name, SqlParameter[] p = null)
{
            DataSet ds = new DataSet();

            using (SqlConnection conn = new SqlConnection(Connstr))
            {
                SqlDataAdapter da = new SqlDataAdapter(sp_name,conn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.CommandTimeout = 300;

                if (p != null)
                    for (int i = 0; i < p.Count(); i++)
                        da.SelectCommand.Parameters.Add(p[i].ParameterName, p[i].SqlDbType, p[i].Size).Value = p[i].Value;

                conn.Open();
                da.Fill(ds); // this is the line that the exception is thrown
                conn.Close();
            }

            return ds;
}
2
  • 1
    I think the exception is clear: Invalid column name 'quantity' means, there is no column named 'quantity'. You might connect to the wrong database or you'll need to create the tables first. Commented Apr 1, 2016 at 8:51
  • Check whether the connection is appropriate Commented Apr 1, 2016 at 8:52

2 Answers 2

4

I have also encountered the same error before, I think the problem was on your stored procedure. Try to check all the declarations, specially your temporary tables. I don't know if it will works on your part. Try to avoid the same temporary tables name. Just try it :)

Sign up to request clarification or add additional context in comments.

2 Comments

yes you are right. I have same temporary table names in my stored procedures
Ahh i see. I'm glad i was able to help you :)
0

Check columns names, columns in stored procedure must be the same as attributes declared in your Model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.