Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I managed to fill the grid view but

C#
protected void btnShow_Click(object sender, EventArgs e)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CStringCRM"].ToString();

            DataTable dt = new DataTable();
            SqlDataReader dr = null;

            using (SqlConnection conn = new SqlConnection(conStr))
            {
                // Creating insert statement
                string sql = string.Format(@"SELECT * FROM UserInfo");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                dr = cmd.ExecuteReader();

                dt.Load(dr);

                conn.Close();
                cmd = null;

            }

            GView.DataSource = dt;
            GView.DataBind();

            txtFName.Text = dr["UFName"].ToString();
                       
        }


I also want to display the data in textbox which are txtFName but these code does not working txtFName.Text = dr["UFName"].ToString();.

Please help
Posted
Updated 28-Jul-15 23:25pm
v2
Comments
Afzaal Ahmad Zeeshan 29-Jul-15 5:26am    
Why doesn't it work? Did you try to debug the application. Also, please let me know what does it display.
Altaful Haq 29-Jul-15 5:34am    
invalid Operational exception was unhandled by User

{"Invalid attempt to call MetaData when reader is closed."}
lukeer 29-Jul-15 5:35am    
Which part does not work?
Have you checked What data type is dr["UFName"]? If it's an empty string, it may get displayed but looks as if nothing has happened.
I'd start by setting a breakpoint on ExecuteReader() to see what's being fetched from the db.
Altaful Haq 29-Jul-15 5:47am    
it have value which i can see in gridview but not her and the is only one record

1 solution

Hi Use break point and check in dt,in that datatable got UFName value ?
and u are using
C#
Select * from table name
so in that u retrieve lot of records,then u need to specify which row.

C#
dt.Rows[0]["UFName"].ToString();
 
Share this answer
 
v2
Comments
Altaful Haq 29-Jul-15 5:44am    
what does you means by break point
Aravindba 29-Jul-15 6:01am    
Debug ur code or trace ur codes = Break point

https://www.youtube.com/watch?v=0-TS8SDUmMY
https://www.youtube.com/watch?v=7ab4z9u7Q_I

check this links

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900