ankit gaur

ankit gaur

  • 1.6k
  • 248
  • 5.3k

Fill Database value in Label inside Gridview

Mar 7 2019 6:38 AM
Hi ,
 
I have a gridview ..In Gridview 4 Dropdown and 3 Labels. i want to fill Sql Server Database value in these dropdowns and labels.
 
Now the problem is that Only in one Label [unit fill from the database] . other two labels not fill.
  1. protected void Gridview3_RowDataBound(object sender, GridViewRowEventArgs e)  
  2. {  
  3. if (e.Row.RowType == DataControlRowType.DataRow)  
  4. {  
  5. SqlCommand cmd = new SqlCommand("Select ValueId,Value1,Unit2,Unit1,Unit21,Value2,Value21,Discription from tblMatValueTemp1 where ItemId = '" + lblItem.Text + "' ", myconn);  
  6. cmd.CommandType = CommandType.Text;  
  7. SqlDataAdapter da = new SqlDataAdapter();  
  8. da.SelectCommand = cmd;  
  9. DataTable dt = new DataTable();  
  10. DataSet ds = new DataSet();  
  11. da.Fill(dt);  
  12. if (dt.Rows.Count > 0)  
  13. {  
  14. DropDownList DropDownList1 = (DropDownList)e.Row.FindControl("ddlValue1");  
  15. DropDownList DropDownList2 = (DropDownList)e.Row.FindControl("ddlValue2");  
  16. DropDownList DropDownList3 = (DropDownList)e.Row.FindControl("ddlDisp");  
  17. DropDownList DropDownList4 = (DropDownList)e.Row.FindControl("ddlValue21");  
  18. Label lbl1 = (Label)e.Row.FindControl("lblSValues");  
  19. Label lbl2 = (Label)e.Row.FindControl("lblcr1");  
  20. Label lbl3 = (Label)e.Row.FindControl("lblcr2");  
  21. DropDownList1.DataSource = dt;  
  22. DropDownList1.DataTextField = "Value1";  
  23. DropDownList1.DataValueField = "Value1";  
  24. DropDownList1.DataBind();  
  25. lbl1.Text = dt.Rows[0]["Unit1"].ToString(); --------Label 1  
  26. DropDownList2.DataSource = dt;  
  27. DropDownList2.DataTextField = "Value2";  
  28. DropDownList2.DataValueField = "Value2";  
  29. DropDownList2.DataBind();  
  30. DropDownList4.DataSource = dt;  
  31. DropDownList4.DataTextField = "Value21";  
  32. DropDownList4.DataValueField = "Value21";  
  33. DropDownList4.DataBind();  
  34. lbl2.Text = dt.Rows[0]["Unit2"].ToString();--------Label 2 [This label is blank no value fill]  
  35. lbl3.Text = dt.Rows[0]["Unit21"].ToString();--------Label 3 [This label is blank no value fill]  
  36. DropDownList3.DataSource = dt;  
  37. DropDownList3.DataTextField = "Discription";  
  38. DropDownList3.DataValueField = "Discription";  
  39. DropDownList3.DataBind();  
  40. }  
  41. }  
  42. }  

Answers (1)