Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having a dropdownlist and two text box and i have to bind the data from these to a grid on a button click. here is my code ...but the data is not displaying in grid ...but on debugging data is retrieved on corresponding data table comns ...could some one help me out
?






DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt = new DataTable();
dt.Columns.Add("Product", typeof(string));
dt.Columns.Add("Watt", typeof(string));
dt.Columns.Add("Duration", typeof(Int32));
GridView1.DataSource = dt;
GridView1.DataBind();
Session["dt"] = dt;
}
}

protected void submit_Click(object sender, EventArgs e)
{
DataTable dt2 = (DataTable)Session["dt"];
DataRow dr = dt2.NewRow();
dr["Product"] = DropDownList1.Text;
dr["Watt"] = Txtwatt.Text;
dr["Duration"] = TxtDuration.Text;

dt2.Rows.Add(dr);
GridView1.DataSource = dt2;
GridView1.DataBind();
Posted

C#
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if(Session["dt"]!=null)
{
  dt=(DataTable)Session["dt"];
}
else
{
  dt = new DataTable();
  dt.Columns.Add("Product", typeof(string));
  dt.Columns.Add("Watt", typeof(string));
  dt.Columns.Add("Duration", typeof(Int32));
}

GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void submit_Click(object sender, EventArgs e)
{
DataTable dt2 =new DataTable();
if(Session["dt"]!=null)
{
  dt2=(DataTable)Session["dt"];
}
DataRow dr = dt2.NewRow();
dr["Product"] = DropDownList1.Text;
dr["Watt"] = Txtwatt.Text;
dr["Duration"] = TxtDuration.Text;

dt2.Rows.Add(dr);
Session["dt"]=dt2;

}
 
Share this answer
 
v6
Comments
MohRizwan 20-Jul-13 1:53am    
thanks mr.Zafar .. i change the code as you said but igot an error
Line 37: DataRow dr = dt2.NewRow();
Object reference not set to an instance of an object.
Zafar A khan 20-Jul-13 2:11am    
just change this

protected void submit_Click(object sender, EventArgs e)
{
DataTable dt2=new DataTable();
if(Session["dt"]!=null)
{
dt2= (DataTable)Session["dt"];
}
DataRow dr = dt2.NewRow();
dr["Product"] = DropDownList1.Text;
dr["Watt"] = Txtwatt.Text;
dr["Duration"] = TxtDuration.Text;

dt2.Rows.Add(dr);
Session["dt"]=dt2;

}
MohRizwan 20-Jul-13 2:49am    
thanks for your reply ..but the code is not working
dr["Watt"] = Txtwatt.Text.ToString();
error at this line :
Column 'Product' does not belong to table .
Zafar A khan 20-Jul-13 2:53am    
End the session or rerun the application
because in old there is no column with name product.
or wash the history of your browsers clean the session
then restart your application this will fix the error
Its a sample , try this as per your requirement

C#
   DataTable dtList = new DataTable();
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           dtList.Columns.Add("Name");

           ViewState["Data"] = dtList;
       }

   }


 void SaveAndBindDatatable(DataTable dt)
    {
        ViewState["Data"] = dtList;
        GridView1.DataSource = dtList;
        GridView1.DataBind();
    }
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
        dtList = (DataTable)ViewState["Data"];
        dtList.Rows.Add(txtValue.Text.Trim());
        SaveAndBindDatatable(dtList);
}


Or follow this tip i've used datalist instead of gridview to make a custom auto suggestion control.
Auto Suggestion Control Like in GMail/Hotmail/Facebook[^]
 
Share this answer
 
v2

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