Data Table

Asked By samantha jyesta
08-Sep-10 01:56 PM
Earn up to 0 extra points for answering this tough question.
Hi all
I've  3 labels with 3 textboxes , a button and a grid view; when i click on the button the values of the textboxes must be shown in the gridview and the data that we entered in the textboxes must be cleared..i finished doing it..but my requirement is that when i enter the data to the textboxes for second time ,the values must be added to the gridview..

but when do this, my new values getting replacing in the gridview..

***Here im not using Any DataBase Connections***
plz help me out...

  re: Data Table

bryan tugade replied to samantha jyesta
08-Sep-10 09:15 PM
Hi,

Well the problem is that the data that has been bind to your gridview are not stored in your database. The tendency is that when the postback event triggers, it refresh all the control in the page. I suggest create a database then put the data in that database in able for you to retrieve it anytime you want.

Thats it. Happy Coding.

  re: Data Table

[ Kirtan ] replied to samantha jyesta
09-Sep-10 12:07 AM
Snippet
 protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      DataTable dt = null;
      dt = new DataTable();
      dt.Columns.Add("Username");
      dt.Columns.Add("Password");
      Session["MyTable"] = dt;
    }
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
    DataTable dt = (DataTable)Session["MyTable"];
    string username = TextBox1.Text;
    string password = TextBox2.Text;
    DataRow dr = dt.NewRow();
    dr[0] = username;
    dr[1] = password;
    dt.Rows.Add(dr);
    GridView1.DataSource = dt.DefaultView;
    GridView1.DataBind();
  }

  re: Data Table

samantha replied to [ Kirtan ]
09-Sep-10 04:43 AM
Thank u so much friend..
Actually I did the same but two modifications are there in my coding. I kept the data in the session but i got confused where to get the data from the session..Can u plz tell me about Sessions..if possible...

Thank u,
$@M!
  re: Data Table
[ Kirtan ] replied to samantha
09-Sep-10 05:52 AM
you can get the data From Session  and Write Data to Session at any page even you can retrieve that DataTable on other page too .,

to store the Data

Session["VarName"] =  your Data

when retriving

<DataType> v1 =  <DataType>Session["VarName"]


while retrieving the data Casting is needed as Session Stores data as Object so we need to Convert Data While retriving it from session
  re: Data Table
samantha replied to [ Kirtan ]
09-Sep-10 06:22 AM
Thank u friend..
Can u tell me about image uploading in asp.net(c#). The path of the image should be stored as a binary format and the image should displayed when i click on a button.
  re: Data Table
[ Kirtan ] replied to samantha
09-Sep-10 06:28 AM
you mean you want to store an Image as Binary In SQL Server Database and Want to Retrieve it when you click on Button ?
  re: Data Table
[ Kirtan ] replied to samantha
09-Sep-10 07:20 AM
In Database i Created Table ImageTable with Two Column

ID and Image

ID = type of int
and
Image is type of varbinary(MAX)

 protected void btnInsertData_Click(object sender, EventArgs e)
  {

    if (FileUpload1.HasFile == true)
    {
      int id = int.Parse(txtID.Text);

      /* read Image Data into byete array */
      byte[] ImageData = FileUpload1.FileBytes;

      /* Connect to database and Write Data into Database */

      SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
      con.Open();
      SqlCommand comm = new SqlCommand("insert into ImageTable(ID,Image) values(@ID,@Image)", con);
      comm.Parameters.AddWithValue("@ID", ID);
      comm.Parameters.AddWithValue("@Image", ImageData);
      comm.ExecuteNonQuery();
      con.Close();
    }
  }

  protected void btnReadImageData_Click(object sender, EventArgs e)
  {
    int ID = int.Parse(txtID.Text);
    /* Connect to database and Write Data into Database */

    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    SqlCommand comm = new SqlCommand("select Image from ImageTable where id=1", con);
    comm.Parameters.AddWithValue("@ID", ID);
    object ImageBinary = comm.ExecuteScalar();
    Response.BinaryWrite((byte[])ImageBinary);
    con.Close();
  }
Create New Account