Insert DateTime from ASP.NET into Stored Proc - c#

I am trying to insert a datetime stamp from an asp.net application into a db, but i keep recieving an error ::
The column "W_Date" cannot be modified
because it is either a computed column
or is the result of a UNION operator.
Can someone shed some light on this. Also, I automate the datetime in my asp in a label, then pull the text for the sql insert with the following code.
lblDate.Text = DateTime.Now.ToString();
Is this correct?
Below is my stored Proc code:
#date date
AS
BEGIN
INSERT INTO tbl_Wiki(W_Title, C_ID, W_Date)
VALUES (#title, #c_ID, GETDATE())
END

It doesn't sound like there's anything wrong with your ASP.NET code, assuming the W_Title is the correct column name. Do you have enough control over the schema to see if W_Title is a computed column or not?
Also, it looks like you're passing in "date" as a parameter to your proc, but you're not using it in the INSERT statement. If W_Date is a computed column to always be the current date, you should remove the W_Date paramter in your insert statement.

Check the definition of the column W_Date in table tbl_Wiki. It's probably a computed column, which means you can't modify it.
You could use this query to check if a column is computed:
select is_computed
from sys.columns
where object_id = object_id('tbl_Wiki')
and name = 'W_Date'
Example of a computed column:
create table Sample (a int, b int, c as sqrt(a*a+b+b))
Here, c is computed every time the row is retrieved. You can't overrule the calculation by specifying a value during an insert or update.

Related

DbDataAdapter with value from a db function

is it possible to do a tableadapter update with a datarow[]
and for a datetime column the value is set to a db function ea getdate(), userDefinedFunctionReturnDateTime() etc..
so not fixed values from a datarow[]
INSERT INTO X(A,B,C)VALUES(1, '2015-01-01 00:00', 'name')
but function values
INSERT INTO X(A,B,C)VALUES(1, getdate(), 'name')
something like datarow.ItemArray[1] = "getdate()"; ?
Yes, just set the insert value based on the command your SQL database recognizes and it should work.
Ex: MySQL could use NOW()
SQL-Server could use GetDate()
all exactly as you have sampled. The only thing is, you would NOT be using a parameter value, just build your insert statement with that fixed context INSTEAD of an actual parameter place-holder.
Is there a specific way you are building your insert statements? or having them auto-generated somehow?

How to insert long type data in to SQL SERVER Big int type

HI recently im working on a project . Its easy for INT but due to overflow situation I want to use bigInt in sql column and i'm sending him a LONG type data like Long l="Somthing value"
and inserting like Insert into Table value(Given Value)
But why there is an error saying "Given value is not matching with the table definition "
Give me a sample code if possible
Make sure you are inserting correct values in table. Suppose you have three column and you are inserting four values then error will occur.
suppose you have following column
i.e. column1, column2 column3 etc. and you insert statement
insert into tablename(column1, column2, column3) values ('somevalue','somevalue','somevalue')
Here value is based on datatype that you have mentioned while creating a table.
Mention the column name in the insert statement, what ever the columns are you going to insert
Since the field in the table is of type bigint, it is not possible to store a long value there. You could change your application to use an integer for the value, or you can cast the long to an int64 (eq to a bigint in sql) this will drop all the decimal values of the long.
Specify column names when inserting.
into Table(Give_Value_Column) Values(Given Value)
command = Insert into Table value(Given=#Value)
command.perameter.add(#value, datatype, values)

Get last generated ID and add it to another table ASP.NET

How can I get get generated ID from primary key and then add it to another table in ASP.NET via SCOPE_IDENTITY?
For example:
Last generated ID on column NRRENDOR is number 26, I have deleted the rows. Now when I add datas to the database the nexy generated ID on NRRENDOR will be number 27. That number I want it to add to column NRD.
In your INSERT code, assuming it's in a stored procedure, using SCOPE_IDENTITY will get you the last identity that was inserted, which you can either reuse in the stored procedure or return to your app to use in another statement.
Some dummy SQL to demonstrate:
INSERT INTO NRRENDOR(SomeColumn) VALUES(1)
DECLARE #LastID int
// set #LastID to the last id inserted
SELECT #LastID = SCOPE_IDENTITY()
// to use in same procedure
INSERT INTO NRD (SomeColumn) VALUES(#LastID)
// to return it to code - or you could use an output parameter
SELECT #LastID
What has this got to do with ASP.NET? SQL would suffice.
INSERT INTO [Table2]( NRD)
SELECT MAX(NRRENDOR)
FROM Table1
' WITH (ROWLOCK, XLOCK, HOLDLOCK)
The correct way to do it will be to use #SCOPE_IDENTITY after you perform insertion as Tanner suggested. It will be worth noting that there is another way to get the current identity, ie, IDENT_CURRENT. You can use it like this
SELECT IDENT_CURRENT('Table1') + 1 as Current_Identity
Please note this too
Be cautious about using IDENT_CURRENT to predict the next generated
identity value. The actual generated value may be different from
IDENT_CURRENT plus IDENT_INCR because of insertions performed by other
sessions.
This comment from marc_s sums it all. How to get the next identity value from SQL Server

Is there any way to use SCOPE_IDENTITY if using a multiple insert statement?

I will import many data rows from a csv file into a SQL Server database (through a web application). I need the auto generated id value back for the client.
If I do this in a loop, the performance is very bad (but I can use SCOPE_IDENTITY() without any problems).
A more performant solution would be a way like this:
INSERT INTO [MyTable]
VALUES ('1'), ('2'), ('3')
SELECT SCOPE_IDENTITY()
Is there any way to get all generated IDs and not only the last generated id?
No, SCOPE_IDENTITY() only gives you the one, latest inserted IDENTITY value. But you could check out the OUTPUT clause of SQL Server ....
DECLARE #IdentityTable TABLE (SomeKeyValue INT, NewIdentity INT)
INSERT INTO [MyTable]
OUTPUT Inserted.Keyvalue, Inserted.ID INTO #IdentityTable(SomeKeyValue, NewIdentity)
VALUES ('1'), ('2'), ('3')
Once you've run your INSERT statement, the table variable will hold "some key value" (for you, to identify the row) and the newly inserted ID values for each row inserted. Now go crazy with this! :-)

SQL Query to compare 2 rows and store the differences in variables C#?

I am currently working on a project that is looking at having a database that will store only 2 rows of data in it. This database will have new rows added to it all the time and the top row will be deleted when a new row is entered making sure there are always 2 rows.
What I am looking to do is to compare the top row of data and the bottom row of data and then store the data that is different in variables. I only want to store the data that is different from the bottom row as the top row will always be the oldest sets of data and I would no longer need to handle that.
I can do this all on the C# side but I was wondering if there is a way to do most of this on the SQL side of things? Thanks for the help :-)
thanks for the clearification, let assume this table:
create table temp_monitor(id int, stamp datetime, #newTemp1 float, #newTemp2 float)
then this stored procedure should do the trick
create proc updateTempMonitor(temp_oven_1 float, temp_oven_2 float)
as
-- save the values where about to replace
declare #id int, #t1 float, #t2 float
select top 1 #id = id, #t1 = temp_oven_1, #t2 = temp_oven_2 from temp_monitor order by stamp
-- update the oldest
update updateTempMonitor set temp_oven_1 = #newTemp1, temp_oven_2 = #newTemp2 where id = #id
-- return data to caller (c#)
select #t1, #t2
It is possible in both side C# and SQL,
SQL side you need to write SP or something which will return you pivot with the diff in the form of column name and changed columns.
look here for more . SQL Rows to Columns

Categories