Click here to Skip to main content
15,883,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to insert value (X) to a database, but now i want to compare that,
if X last value is not equal to X present value then INSERT to a database table.
I really dont know what will be its syntax. Thanks
Posted
Comments
Naveen.Sanagasetti 31-Jan-13 0:19am    
hmmmmm, Could you please elaborate your question...
ontheline89 31-Jan-13 0:22am    
i am trying to save values coming from COM Port to a database. when i store these value (X) then many time its value do not change and give same values but after some time its values are changed. I just want to insert the different values in a database.
_Maxxx_ 31-Jan-13 0:26am    
Do you really mean you are inserting X - or are you updating a row with a new value of X.
If you are inserting a new row, do you mean you only want to insert the new row if the value of X is not equal to the previous row's X value? If so, what do you mean by 'previous row'? Is it the last row inserted by this user, by any user , with the next lowest key value?
explain more and I am sure someone can give you some SQL to point you in the right direction!
ontheline89 31-Jan-13 0:29am    
Yes i am inserting new rows when i get a value of X variable from COM port.
I need to save X to a new row only when its value change from the previous row value.

Using SQL

SQL
Create proc asp_savetable
(@newValue varchar(10)
@id int)
as
BEGIN
   Declare @oldvalue varchar(10)
   Select @oldvalue =name from table1 where id=@id
   If @oldvalue<>@newvalue
      insert into table1 values (@id,@newvalue)
END


here Id,name are two columns in my table table1

Using c#

  using (SqlConnection conn = new SqlConnection(connString))
    {
		string oldvalue="";
		SqlCommand cmd=new SqlCommand("Select name from employee where id=@id",conn)
		cmd.Parameters.Add("@id", SqlDbType.Int);
		cmd.Parameters["@id"].Value = empid;
        	try
        	{
            		conn.Open();
            		oldvalue= cmd.ExecuteScalar().ToString();
        	}
       		 catch (Exception ex)
       		 {
            		Console.WriteLine(ex.Message);
        	}
	}

If (oldvalue!=newvalue)
{
	  using (SqlConnection conn = new SqlConnection(connString))
    {
		string oldvalue="";
		SqlCommand cmd=new SqlCommand("insert into employee values(@id,@name)",conn)
		cmd.Parameters.Add("@id", SqlDbType.Int);
		cmd.Parameters["@id"].Value = empid;
		cmd.Parameters.Add("@name", SqlDbType.VarChar);
		cmd.Parameters["@name"].Value = newvalue;
        	try
        	{
            		conn.Open();
            		cmd.NonExecuteQuery();
        	}
       		 catch (Exception ex)
       		 {
            		Console.WriteLine(ex.Message);
        	}
	}	
}
 
Share this answer
 
v2
Comments
ontheline89 31-Jan-13 0:49am    
Thanks for your reply.
I need to use these line in my C# Code ?
Santhosh Kumar Jayaraman 31-Jan-13 1:03am    
I have added for c# code also
ontheline89 31-Jan-13 1:05am    
Thank you so much for your answer.
It sounds by your comments like you are a novice - so please fogive me if this isn't the case:

In your C# code you probably have some code that does the following

Get X From COM port

Write X to database table


So, you need a field in your class to store the 'old' value of X.
C#
private int oldX = -1;


I initialise it with -1 on teh assumption that it is not possible to receive -1 from the COM port...

Now your code can change to...

C#
Get X from COM PORT

if (X != oldX)
{
    oldX = X;
    write X to database table
}
 
Share this answer
 

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