0

I am using following GridView control in WebForm

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" ForeColor="#333333" GridLines="None" 
            style="z-index: 1; left: 276px; top: 192px; position: absolute; height: 133px; width: 187px">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField HeaderText="userid" />
                <asp:BoundField HeaderText="password" />
                <asp:BoundField HeaderText="usertype" />
                <asp:BoundField />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

and below is the code in C# to display data in this gridview control from MySql data source.

protected void Page_Load(object sender, EventArgs e)
        {
            String constring = "Driver={MySQL ODBC 5.1 Driver};server=localhost;uid=root;database=db_dcii;port=3306";
             DataTable dt = new DataTable();
             OdbcConnection connection = new OdbcConnection(constring);  
         try  
         {  
   
             connection.Open();              
             OdbcCommand sqlCmd = new OdbcCommand();  
             sqlCmd.Connection = connection;  
             sqlCmd.CommandType = CommandType.StoredProcedure;  
             sqlCmd.CommandText = "Select * from login";  
             OdbcDataAdapter sqlDa = new OdbcDataAdapter(sqlCmd);  
             sqlDa.Fill(dt);  
             if (dt.Rows.Count > 0)  
             {  
                 GridView1.DataSource = dt;
                 GridView1.DataBind();  
             }  
         }  
         catch (Exception ex)  
         {  
             string msg = "Fetch Error:";  
             msg += ex.Message;  
               
         }  
         finally  
         {  
             connection.Close();  
         }  
        }

when I run the build the webform nothing is displayed on web browser. I have made some entries in the db_dcii.login table which has three fields. I have been trying this since 3 hours but still not successful.

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by Ramesh S
0

You are using a SELECT statement to fetch records from database.

But you have specified that the OdbcCommand.CommandType is StoredProcedure which is wrong. It should be Text type. Your code should look like as

protected void Page_Load(object sender, EventArgs e)
        {
            String constring = "Driver={MySQL ODBC 5.1 Driver};server=localhost;uid=root;database=db_dcii;port=3306";
             DataTable dt = new DataTable();
             OdbcConnection connection = new OdbcConnection(constring);  
         try  
         {  
   
             connection.Open();              
             OdbcCommand sqlCmd = new OdbcCommand();  
             sqlCmd.Connection = connection;  
             [B]sqlCmd.CommandType = CommandType.Text;  [/B]             sqlCmd.CommandText = "Select * from login";  
             OdbcDataAdapter sqlDa = new OdbcDataAdapter(sqlCmd);  
             sqlDa.Fill(dt);  
             if (dt.Rows.Count > 0)  
             {  
                 GridView1.DataSource = dt;
                 GridView1.DataBind();  
             }  
         }  
         catch (Exception ex)  
         {  
             string msg = "Fetch Error:";  
             msg += ex.Message;  
               
         }  
         finally  
         {  
             connection.Close();  
         }  
        }

Also put a break point at dt.Rows.Count to see if it returns any records.

Edited by Ramesh S: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.