Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
C#
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

Make a new field in your table named like CreatedDate of DateTime datatype and set its default value to getdate().
 
Share this answer
 
v2
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
 
Share this answer
 
Comments
musiw 25-Jul-12 4:01am    
i dont get it.can u explain how to implement it?
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())
 
Share this answer
 
C#
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();
    }
 
Share this answer
 
v2
Comments
musiw 25-Jul-12 20:39pm    
i already try.but nothing display.no error.1 thing problem i look in database getdate() is stored as in format '2012/07/26'.i changed query into date like '%26/07/2012%' nothing display.
hi musiw
oncetry like this


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


Thanks&Regards
sandeep
 
Share this answer
 
Comments
musiw 25-Jul-12 20:23pm    
i got this error:

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

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