Solved

TimeStamp/Datagrid update issue

Posted on 2004-08-23
6
448 Views
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]);
                  inv=box.Text;

                  // 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("@Part_Key",SqlDbType.Char);
                  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];

SCom.Connection.Open();

                  try
                  {
                        SCom.ExecuteNonQuery();
                        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");
                        else
                              Response.Write ("ERROR: Could not update record, please ensure the fields are correctly filled out");
                        

                  }

                  SCom.Connection.Close();

                  BindIt();
            }
0
Comment
Question by:devlins
[X]
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
6 Comments
 
LVL 3

Accepted Solution

by:
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.
0
 

Author Comment

by:devlins
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???
0
 
LVL 3

Expert Comment

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

Assisted Solution

by:EBatista
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("@Part_Key",SqlDbType.Char);
SCom.Parameters["@Part_Key"].Value=key;

SCom.Parameters.Add("@Inventory_Status", SqlDbType.Char);
SCom.Parameters["@Inventory_Status"].Value=inv;

SCom.Parameters.Add("@Reserve_Time", SqlDbType.DateTime);
SCom.Parameters["@Reserve_Time"].Value=ToDa;
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

623 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