locked
Alias Name and Datatable in C# RRS feed

  • Question

  • User-1251655565 posted

    i use the following stored procedure in SQL :

    select sum(credit) as x from table1 as CC 
    inner join table2 as C on C.CID=CC.CID
    where CC.EN='123' and CC.Sem='20171'

    I create a data table in visual studio and i got this error : Column 'x' does not belong to table .

        public DataTable GetL(string EN,string Sem)
        {
            SqlDataAdapter da = new SqlDataAdapter("getload", conn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("@EN", EN);
            da.SelectCommand.Parameters.AddWithValue("@Sem", Sem);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
    
        }

     DataTable DC = DA.GetL(sem.Text, Convert.ToString(Session["EN"]));
     Label2.Text = DT.Rows[0]["x"].ToString();

    the problem is the alias name x, the data table and SQL query is running well. Any help ?

    Thursday, December 22, 2016 11:35 PM

All replies

  • User-271186128 posted

    Hi asp.net King,

    Column 'x' does not belong to table .

    You can debug the columns property in datatable to find out what the column name is.

    For example:

    This is my store procedure:

    SELECT SUM(UnitPrice) as x from [Order Details] as cc
    inner join Orders as c on c.OrderID=cc.OrderID
    where CC.ProductID=@ProductID
    
    

    The GetL() and Page_Load():

    public DataTable GetL() 
            {
                int id = 11;
                SqlDataAdapter da = new SqlDataAdapter("OrderDetail", ConfigurationManager.ConnectionStrings["NorthwindConnectionString1"].ConnectionString);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.AddWithValue("@ProductID", id);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
    }
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = GetL();                
                    var a = dt.Rows[0]["x"].ToString();
                    var b = dt.Columns;
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
    }
    

    When I debug the Columns the result is:

    You can see the column’s name is displayed

    Best regards,
    Dillion

     

    Friday, December 23, 2016 5:45 AM