Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

i
want to display a data in gridview what i have entered in textboxes by using data table not with databinding

and i have taken the following control in asp.net application

XML
<form id="form1" runat="server">
   <div>

       <br />
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
      
       <asp:GridView ID="GridView1" runat="server">
       </asp:GridView>
       

   </div>
   </form>


and in button click event i have add the following code

C#
DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Address");
        dt.Columns.Add("Password");
        DataRow dr = dt.NewRow();
        dr["Name"] = TextBox1.Text;
        dr["Address"] = TextBox2.Text;
        dr["Password"] = TextBox3.Text;
        dt.Rows.Add(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();

Then when i run the asp.net application then when i entered the followning information in textboxes then i click button

the following record will added in griview but the problem is that
when i enter next record the previous record was not visible the new record will be visible

so help me out
Posted
Updated 28-Dec-11 0:10am
v3
Comments
Karthik Harve 28-Dec-11 6:05am    
[Edit] pre tags added.
Slacker007 28-Dec-11 6:10am    
Edits: Shortened title because it was already mentioned int he body of your question.

You will have to maintain the datatable in the Session. And then add the new records to the datatable in the session every time.

When you want to bind the datatable to the grid, then pull the datatable from the session.

My other suggestion is that the databind logic should be in Page_load event rather than the click event
 
Share this answer
 
v2
This is happening because you are creating new instance of DataSet/DataTable everytime when you click button.
Declare a DataSet/DataTable at Class Level and create its instance in constructor.
Now add your rows to the table and bind it to gridview, it'll work.

Other method is on button click, cast the DataSource property of gridview into a DataTable and add row to that DataTable and bind it back to gridview.

Please let me know if you need more explanation.
 
Share this answer
 
Use ViewState to save datatable, it store on client side.

C#
DataTable dt = new DataTable();
if(dt == null)
{
   dt.Columns.Add("Name");
   dt.Columns.Add("Address");
   dt.Columns.Add("Password");
   ViewState("Dt") = dt;
}
else
{
   dt = (DataTable)ViewState("Dt");
   DataRow dr = dt.NewRow();
   dr["Name"] = TextBox1.Text;
   dr["Address"] = TextBox2.Text;
   dr["Password"] = TextBox3.Text;
   dt.Rows.Add(dr);
   GridView1.DataSource = dt;
   GridView1.DataBind();
}
 
Share this answer
 
v2
Comments
Reshma89 28-Dec-11 7:51am    
thank u for r response
when try with this code when i run the asp.net application the following error will be occur
Compiler Error Message: CS0103: The name 'ViewState' does not exist in the current context
Reshma89 28-Dec-11 8:01am    
hello Sarvesh
thank u for r response
when try with this code when i run the asp.net application the following error will be occur
Compiler Error Message: CS0103: The name 'ViewState' does not exist in the current context
bbirajdar 28-Dec-11 8:34am    
You must have added this code into a class. Add this code in the code behind where the namespace System.Web.UI; is available by default and it will work.
Reshma89 29-Dec-11 0:24am    
hello Birajdar

Hello birajdar .i did not get u r answer
Reshma89 29-Dec-11 0:27am    
hello Sarvesh
i change the following instead of ViewState("Dt") i assign ViewState["Dt"]
when i try with this code when run the application it can run and when i pass values in textboxes and i clik button


the following error will occur

Object reference not set to an instance of an object.


so pls give u r suggestion
change your code like this..

C#
DataTable dt = (DataTable)GridView1.DataSource;
if (dt == null)
{
   DataTable dt = new DataTable();
   dt.Columns.Add("Name");
   dt.Columns.Add("Address");
   dt.Columns.Add("Password");
}
DataRow dr = dt.NewRow();
dr["Name"] = TextBox1.Text;
dr["Address"] = TextBox2.Text;
dr["Password"] = TextBox3.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();

hope it works..
 
Share this answer
 
Store datatable in session.

If session found then use datatable from the session else create new object of datatable.


 DataTable dt;
if(Session["dt"]!=null)
{
dt = (Datatable)Session["dt"];
}else
{
dt = new DataTable();
 dt.Columns.Add("Name");
        dt.Columns.Add("Address");
        dt.Columns.Add("Password");
}
       
       
        DataRow dr = dt.NewRow();
        dr["Name"] = TextBox1.Text;
        dr["Address"] = TextBox2.Text;
        dr["Password"] = TextBox3.Text;
        dt.Rows.Add(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();
Session["dt"] = dt;



Hope this will help :)

Thanks
Vinod
 
Share this answer
 
Comments
bbirajdar 28-Dec-11 6:18am    
Good one.. My 4 stars
Al Moje 28-Dec-11 20:22pm    
My vote of 3 stars
Session expires, if client page does not postback, suggest to use ViewState
example:
ViewState["dt"] = dt;
Reshma89 29-Dec-11 2:44am    
it can't work

pls send exact code what i have write
Reshma89 29-Dec-11 2:51am    
in class part
i have create a datatable object dt

and in button click event

i have write the following code
if (Session["dt"] != null)
{
dt = (Datatable)Session["dt"];
}
else
{
dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Password");
}


DataRow dr = dt.NewRow();
dr["Name"] = TextBox1.Text;
dr["Address"] = TextBox2.Text;
dr["Password"] = TextBox3.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["dt"] = dt;

then when i run th eapplication when i enter the parameters in textboxes when i click the button

the error will be occur


Column 'Name' does not belong to table .

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Column 'Name' does not belong to table .
]]>



Sub Page_Load
Dim conPubs As SqlConnection
Dim cmdSelect As SqlCommand

conPUbs = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Pubs" )
cmdSelect = New SqlCommand( "Select * From Titles", conPubs )
conPubs.Open()
dgrdTitles.DataSource = cmdSelect.ExecuteReader()
dgrdTitles.DataBind()
conPubs.Close()
End Sub




<title>DataGrid.aspx





 
Share this answer
 
try this
C#
DataTable dt = new DataTable();

    dt.Columns.Add("Name",typeof(string));
    dt.Columns.Add("Address",typeof(string));
    dt.Columns.Add("Password",typeof(string));

dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);
GridView1.DataSource = dt;
    GridView1.DataBind();

}
 
Share this answer
 
v2
Comments
nagendrathecoder 28-Dec-11 6:21am    
I guess this won't solve OP's problem.
It'll always show only current record in gridview coz this code will execute on button click.

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