Click here to Skip to main content
0 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
hi guys,

can you help me on this: i want to insert data into sql database and then i want to bind that latest data into gridview.latest means data that has been inserted in 1 process.not the last row in table.

so i come out with the idea insert a datetime.now into table sql database.and then i want to display the data that has been inserted.but i still got no solution.pls someone help me...
protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection cn0 = new SqlConnection(connectionString);
        cn0.Open(); 
        string l = "insert into approval (name_item, quantity, stock_last,name,staff_no,date) values (@name_item, @quantity, @stock_last, @name, @staff_no, @date)";
        SqlCommand c = new SqlCommand(l, cn0);

        //TextBox8.Text = dt.ToString();

        c.Parameters.Add("@name_item", SqlDbType.VarChar).Value = ddl1.SelectedItem.Text.Trim();
        c.Parameters.Add("@quantity", SqlDbType.Int).Value = TextBox7.Text.Trim();
        c.Parameters.Add("@stock_last", SqlDbType.Int).Value = kuantiti;
        c.Parameters.Add("@name", SqlDbType.VarChar,50).Value = TextBox3.Text.Trim();
        c.Parameters.Add("@staff_no", SqlDbType.Int).Value = TextBox2.Text.Trim();
        c.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Now;// = TextBox8.Text;// = TextBox8.Text;

        cd.ExecuteNonQuery();
        c.ExecuteNonQuery();
    }

protected void Button5_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        conn.Open();

        DateTime dt = DateTime.Now;
        SqlConnection con0 = new SqlConnection(connectionString);
        string s = "select convert(varchar(11), date, 103)as date from approval where staff_no = '" + TextBox2.Text + "'";
        SqlCommand cm = new SqlCommand(s,conn);
        //cm.CommandText = 
        //cm.Connection = con0;
        //con0.Open();
        string name = ((string)cm.ExecuteScalar());
        TextBox8.Text = ((string)cm.ExecuteScalar());
        SqlDataAdapter sql = new SqlDataAdapter(s, conn);
        DataSet ds = new DataSet();

        sql.Fill(ds, "approval");
        GridView1.DataSource = ds.Tables["approval"].DefaultView;
        GridView1.DataBind();
    }

thanx in advance
musiw.
Posted
Updated 24-Jul-12 20:51pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Make a new field in your table named like CreatedDate of DateTime datatype and set its default value to getdate().
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,
Think about adding a new column called "inserted_Order" in your table. Then increment that column value in each insert process. Then you will be able to retrieve record set you have inserted in each process.

Regards
Sebastian
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

if your data type of date column in database is datetime then u can also use the getdate SQL function to insert date.

Try:
insert into approval (name_item, quantity, stock_last,name,staff_no,date) values (@name_item, @quantity, @stock_last, @name, @staff_no, getdate())
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection cn0 = new SqlConnection(connectionString);
        cn0.Open(); 
        string l = "insert into approval (name_item, quantity, stock_last,name,staff_no,date) values (@name_item, @quantity, @stock_last, @name, @staff_no, getDate())";
        SqlCommand c = new SqlCommand(l, cn0);

        c.Parameters.Add("@name_item", SqlDbType.VarChar).Value = ddl1.SelectedItem.Text.Trim();
        c.Parameters.Add("@quantity", SqlDbType.Int).Value = TextBox7.Text.Trim();
        c.Parameters.Add("@stock_last", SqlDbType.Int).Value = kuantiti;
        c.Parameters.Add("@name", SqlDbType.VarChar,50).Value = TextBox3.Text.Trim();
        c.Parameters.Add("@staff_no", SqlDbType.Int).Value = TextBox2.Text.Trim();
        c.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Now;

        c.ExecuteNonQuery();
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        conn.Open();

        DateTime dt = DateTime.Now;
        SqlConnection con0 = new SqlConnection(connectionString);
        string s = "select convert(varchar(11), getdate(), 103)as date from approval where staff_no = '" + TextBox2.Text + "'";
        SqlCommand cm = new SqlCommand(s,conn);
        string name = ((string)cm.ExecuteScalar());
        TextBox8.Text = ((string)cm.ExecuteScalar());
        SqlDataAdapter sql = new SqlDataAdapter(s, conn);
        DataSet ds = new DataSet();

        sql.Fill(ds, "approval");
        GridView1.DataSource = ds.Tables["approval"].DefaultView;
        GridView1.DataBind();
    }
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

hi musiw
oncetry like this


string s = "select convert(varchar(11), date, 103)as date from approval where staff_no = '" + TextBox2.Text + "'" and date=max(date);


Thanks&Regards
sandeep
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190419.4 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100