TimeStamp/Datagrid update issue

Posted on 2004-08-23
Last Modified: 2008-01-16
How would I get the code below to update a database and insert a  current date into the Reserve_Timestamp column?

string inv, dateT;
string key = DGrid.DataKeys[e.Item.ItemIndex].ToString();
TextBox box;
                  box = (TextBox)(e.Item.Cells[1].Controls[0]);

                  // Gets the value the TextBox control in the fourth column
                  box = (TextBox)(e.Item.Cells[8].Controls[0]);
                  dateT= box.Text;


                  string Sq;
                  DateTime ToDa = DateTime.Now;

                  Sq = "Update [Waveplate]" +
                        "set Inventory_Status=@Inventory_Status, Reserve_Timestamp='ToDa'"+
                        "where Part_Key =@Part_Key";

                  SqlCommand SCom = new SqlCommand(Sq, Conn);
                  //SqlParameterCollection CSql = SCom.Parameters;
                  SCom.Parameters.Add("@Inventory_Status", SqlDbType.Char);
                  SCom.Parameters.Add("@Reserve_Time", SqlDbType.DateTime);
            SCom.Parameters["@Part_Key"].Value = DGrid.DataKeys[(int)e.Item.ItemIndex];


                        Response.Write ("<b>Record Updated</b><br>" + Sq);
                        DGrid.EditItemIndex = -1;
                  catch (SqlException E)
                        if (E.Number == 2627)
                              Response.Write("ERROR: A record already exists with the same primary key");
                              Response.Write ("ERROR: Could not update record, please ensure the fields are correctly filled out");



Question by:devlins
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

nhewitt21 earned 250 total points
ID: 11870411
change this:
  Sq = "Update [Waveplate]" + "set Inventory_Status=@Inventory_Status, Reserve_Timestamp='ToDa'"+ "where Part_Key =@Part_Key";

to this:
  Sq = "Update [Waveplate]" + " set Inventory_Status=@Inventory_Status, Reserve_Timestamp=" + ToDa + " where Part_Key =@Part_Key";

that should do it i think, hope this helps you out.

Author Comment

ID: 11870722
what do I need to change with the connection string and parameters in order for the code to work do you see
any errors in the code besides the sql script???

Expert Comment

ID: 11870773
i see no problems with the code, although i don't see a db connections string.
LVL 10

Assisted Solution

EBatista earned 250 total points
ID: 11880409
the @Reserve_Time parameters definitions do not corresponds with the query string you've built, also you have not set the value for @Inventory_Status.
try this:

Sq = "Update [Waveplate]" +
"set Inventory_Status=@Inventory_Status, Reserve_Timestamp=@Reserve_Time where Part_Key =@Part_Key";


SCom.Parameters.Add("@Inventory_Status", SqlDbType.Char);

SCom.Parameters.Add("@Reserve_Time", SqlDbType.DateTime);

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question