0

I am trying to display a database table in a DataGrid but the DataGrid isn't displaying. All I have is a textbox and under it I want the DataGrid to display the table. The textbox is displaying but the DataGrid isn't.

<table>
     <tbody>
        <tr>
          <td class="Header">Public Holiday Date</td>
          <td>
             <asp:TextBox runat="server"  ID="txtDate" rel="datepicker" ></asp:TextBox>
          </td>
        </tr>
        <tr>
          <td>
            <asp:DataGrid runat="server" id="dataGridView1"  AutoGenerateColumns="true"></asp:DataGrid>    
          </td>
        </tr>
       </tbody>
     </table>

The code for getting the data from the database:

private DataSet GetBankHolidays()
    {
        DataSet ds = new DataSet();

        string sql = "proc_GetAllCustomers";

        string query = "SELECT BankHol FROM bankholidays";

        DataTable dt = new DataTable();
        using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, DataUtils.ConnectionStrings["TAT"]))
        {
            adapter.SelectCommand.CommandType = CommandType.Text;
            adapter.SelectCommand.CommandText = query;
            adapter.Fill(dt);
            ds.Tables.Add(dt);
            dataGridView1.DataSource = dt;
            dataGridView1.DataBind();
        }
        return ds;
    }

And I'm calling this method in the page load:

 protected void Page_Load(object sender, EventArgs e)
    {
if (!Page.IsPostBack)
        {
            GetBankHolidays();
        }

    }

4 Answers 4

1

You need to call the DataBind method:-

dataGridView1.DataBind();

Apart from this I would suggest you to separate out the DAL & UI code.

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

2 Comments

that gives the error DataGrid with id 'dataGridView1' could not automatically generate any columns from the selected data source.
try setting AutoGenerateColumns property to true
1

write dataGridView1.DataBind(); below dataGridView1.DataSource = dt;

9 Comments

that gives the error DataGrid with id 'dataGridView1' could not automatically generate any columns from the selected data source.
Set Autogeneratecolumn property of gridview to true in your aspx code
@user123456789 are you sure that Datatable contains any records. ?
@user123456789 not database i am asking does adapter.Fill(dt); contains records ?
try to debug and see what values come in dt
|
1

Your code lacks {dataGridView1.DataBind();} command.

Use the DataBind() method to bind data from a data source to the GridView control.

Comments

0

In your coding you are using stored procedure but you have mentioned command type as text

adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

You need to bind DataSource to the DataGrid

dataGridView1.DataBind();

Final code

using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, DataUtils.ConnectionStrings["TAT"]))
        {
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            adapter.SelectCommand.CommandText = query;
            adapter.Fill(dt);
            ds.Tables.Add(dt);
            dataGridView1.DataSource = dt;
            dataGridView1.DataBind();
        }

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.