Wednesday, March 23, 2011

Why can't I bind a formview to this datasource?

I've got this code here:

SqlCommand CodeStatus = new SqlCommand(SQL, DB);        
DB.Open();
Reader = CodeStatus.ExecuteReader();
FormView1.DataSource = Reader;
FormView1.DataBind();
DB.Close();

It works with a gridview, which then displays the data, but with this formview it doesn't display the data. Why not? The default mode is read only, which should just display the data..right?

From stackoverflow
  • I know this doesn't directly answer your question, but have you tried using a DataTable?

    using(SqlDataAdapter adapter = new SqlDataAdapter(sql,connection))
    {
       DataTable table = new DataTable();
       adapter.Fill(table);
       FormView1.DataSource = table;
       FormView1.DataBind();   
    }
    
  • Does your FormView have an ItemTemplate?

    MSDN seems to imply that although it is possible, you need one.

  • MSDN defines FormvView as "A FormView displays the values of a single record from a data source using user-defined templates."

    So you will need to look at a few things-

    1. 1.Test what your reader has. Does it even have rows. Trivial but a common problem.
    2. Provide the formview with a template in itemtemplate atleast.Because a FormView will not infer the columns if you bind it programmatically.
    3. Try databinding in markup for individual template items like <%# Eval("ColName") %>.
    somacore : Since the gridview worked (and returned exactly one row as expected) I assumed the formview would "know" what I wanted to display as well. It's template building time, I suppose. Thanks!

0 comments:

Post a Comment