Insert into table from select query + variables values - asp.net

I want to insert into the table tb2 (QuestionID, QuestionStem, UserID, ExamID) a set of rows selected randomly from table tb1 (QuestionID, QuestionStem) along with values for the two columns UserID, ExamID which are fixed for one insert query. I have tried this query in webmatrix, but I got an error that # should not be in this location in the insert query statment:
db.Query("INSERT INTO tb2 (QuestionID, QuestionStem, UserID, ExamID) SELECT QuestionID, QuestionStem, #UserID, #ExamID FROM tb2");
Any help is appreciated. I am using webmatrix 3.0 to build my app.
Note, UPDATE statement after insert will not work as there will be simultaneuos users and I want to present the selected row based on UserID and ExamID per user.

You can't accomplish your task with a single parameterized query in Webmatrix and, in my opinion, it's not a good solution create a query concatenating parameters.
A better alternative could be extract from table t1 the records you need and insert them one by one with a foreach loop:
#{
var userId = 25;
var examId = 32;
var sql1 = "SELECT TOP 10 QuestionID, QuestionStem FROM t1 ORDER BY NEWID()";
var sql2 = #"INSERT INTO tb2 (QuestionID, QuestionStem, UserID, ExamID)
VALUES (#0, #1, #2, #3)";
var db = Database.Open("yourDb");
var data = db.Query(sql1);
foreach (var row in data){
db.Execute(sql2, row.QuestionID, row.QuestionStem, userId, examId);
}
}
Edited
If performances are a real concern, maybe the best solution is to migrate data from Sql Server Compact to Sql Server Express.
In this environment you could create a Stored Procedure like
CREATE PROCEDURE AddQuestions #UserID int, #ExamID int
AS
INSERT INTO dbo.tb2 (QuestionID, QuestionStem, UserID, ExamID)
SELECT TOP 10 QuestionID, QuestionStem, #UserID AS UserID, #ExamID AS ExamID
FROM dbo.t2 ORDER BY NEWID()
GO
and recall it in WebMatrix:
#{
var userId = 14;
var examId = 16;
var db = Database.Open("yourDb");
var data = db.Execute("EXEC AddQuestions #UserID = #0, #ExamID = #1",
userId, examId);
}

If I understand you correctly, you could use numbered ordinals for the two parameters. Something like this, but setting the variables to use your data as appropriate of course:
var userId = 0;
var examId = 0;
db.Query("INSERT INTO tb2 (QuestionID, QuestionStem, UserID, ExamID) SELECT QuestionID, QuestionStem, #0, #1 FROM tb1", userId, examId);
You also mentioned you want the rows from tb1 to be selected randomly. You can achieve this by adding ORDER BY NEWID() to the end of the SELECT statement in your query:
db.Query("INSERT INTO tb2 (QuestionID, QuestionStem, UserID, ExamID) SELECT QuestionID, QuestionStem, #0, #1 FROM tb1 ORDER BY NEWID", userId, examId);
If you want to limit it to a number of rows only you can do (for example, with ten rows):
db.Query("INSERT INTO tb2 (QuestionID, QuestionStem, UserID, ExamID) SELECT TOP 10 QuestionID, QuestionStem, #0, #1 FROM tb1 ORDER BY NEWID", userId, examId);

Related

While loop in stored procedure in sql returns only one row to grid view in asp.net

Using while loop in stored procedure works correctly in SQL server but returns only first row to asp.net grid view.
My stored procedure
create procedure [dbo].[DoorDetails]
#emp varchar(16),
#fromdate datetime,
#todate datetime,
#cdate datetime =#fromdate
as
while #cdate<= #todate
Begin
select convert(varchar,(CONVERT(date,#cdate,103)),103) as Date, Empname, min(TransactionDateTime) as EntryTime ,max(TransactionDateTime) as ExitTime,
(DateDIFF (MINUTE,min(TransactionDateTime), max(TransactionDateTime)))/60 as Hours,
(DateDIFF (MINUTE,min(TransactionDateTime), max(TransactionDateTime)))%60 as minutes from
ceptEmpTxn where EmpName = #emp and cast(TransactionDateTime as Date)=cast(#cdate as date) group by empname
SET #cdate = DATEADD(dd,1,#cdate)
end
GO
Result in SQL
-------------------------------------------------------------------------------------------------------------
|Date| |Empname| |EntryTime| |ExitTime| |Hours| minutes|
|14/09/2016| |PRAVEEN KUMAR| |2016-09-14 09:28:13.000||2016-09-14 18:42:14.000 9 14
------------------------------------------------------------------------------------------------------------
|Date| |Empname| |EntryTime| |ExitTime| |Hours| minutes|
|15/09/2016| |PRAVEEN KUMAR| |2016-09-15 09:27:13.000||2016-09-15 17:16:46.000 7 49
-------------------------------------------------------------------------------------------------------------
|Date| |Empname| |EntryTime| |ExitTime| |Hours| minutes|
|16/09/2016| |PRAVEEN KUMAR| |2016-09-16 09:30:33.000||2016-09-16 19:03:14.000 9 33
Headers are repeating each time in sql
Result in Webpage (using Grid view)
------------------------------------------------------------------------
Date| Empname| EntryTime| ExitTime| Hours| minutes|
15/09/2016| PRAVEEN KUMAR| 15-09-2016 09:27:07| 15-09-2016 17:16:46| 7| 49|
My function in asp.net
public DataTable tottime(string empname, DateTime fromdate, DateTime todate)
{
System.Data.SqlClient.SqlConnection myConn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["ceptConnectionString"].ConnectionString.ToString());
DataTable myDt = new DataTable();
System.Data.SqlClient.SqlCommand myCmd = new System.Data.SqlClient.SqlCommand();
myCmd.CommandType = System.Data.CommandType.StoredProcedure;
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
myCmd.CommandText = "DoorDetails";
myCmd.Parameters.AddWithValue("#emp", empname);
myCmd.Parameters.AddWithValue("#fromdate", fromdate);
myCmd.Parameters.AddWithValue("#todate", todate);
da.SelectCommand = myCmd;
try
{
myCmd.Connection = myConn;
da.Fill(myDt);
return myDt;
}
catch (Exception ex)
{
throw;
}
finally
{
myDt = null;
da.Dispose();
myCmd.Dispose();
myConn.Close();
myConn.Dispose();
}
}
How to return all the values from stored procedure also how to have table header only once followed by all the rows.
Possibly you may have to create a table variable and store the result set there and again fetch that result set at the end. Try this.
P.S Please change the data types of table variable (#t) columns accordingly. So that there will not be any troubles.
create procedure [dbo].[DoorDetails]
#emp varchar(16),
#fromdate datetime,
#todate datetime,
#cdate datetime =#fromdate
as
while #cdate<= #todate
Begin
DECLARE #T TABLE
(
[Date] Date
,Empname VARCHAR(200)
,EntryTime DATETIME
,ExitTime DATETIME
,[Hours] INT
,[minutes] INT
)
INSERT INTO #T
select convert(varchar,(CONVERT(date,#cdate,103)),103) as Date
, Empname, min(TransactionDateTime) as EntryTime
,max(TransactionDateTime) as ExitTime
,(DateDIFF (MINUTE,min(TransactionDateTime), max(TransactionDateTime)))/60 as Hours
,(DateDIFF (MINUTE,min(TransactionDateTime), max(TransactionDateTime)))%60 as minutes
from
ceptEmpTxn where EmpName = #emp and cast(TransactionDateTime as Date)=cast(#cdate as date) group by empname
SET #cdate = DATEADD(dd,1,#cdate)
end
SELECT * FROM #T
GO

Insert into 3 tables that are inter-dependent

I have 3 tables-
1. Country (CountryName, CID (PK- AutoIncrement))
2. State (SID(PK- AutoIncrement), StateName, CID (FK to Country)
3. City (CityName, CID, SID (FK to State)
Now I need to insert only the name into the three tables with CountryName, StateName and CityName.. The IDs need to get updated.
How'd I do this?
Thanks,
you can use stored procedure to that inside the stored procedure you can first insert into the country table :
Insert into Country ( CountryName) VALUES (#CountryName)
DECLARE #cid as INTEGER = ##IDENTITY
then use SELECT ##IDENTITY in the secound insert like this :
Insert into State( StateName, cid ) values (#StateName, #cid)
DECLARE #SID as INTEGER = ##IDENTITY
and use the same in the third insert statment :
Insert into City ( CityName, CID,SID ) values (#CityName,#CID,#SID )
that is all what you need
If you would like to use linq to SQL
var country = new Dbml.Country();
country.Name = "countryname";
var state = new Dbml.State();
state.Country = country;
state.Name = "stateName"
var city = new Dbml.City();
city.State= state;
city.cityName = "cityName";
context.SubmitChanges();

Output parameter used to insert a row in same table

Thanks for your help Guys
I am working on a Asp.net based project and my requiremnent is to generate a PARTNO with the combination of MaterialType+ProductID and - 4digit random number
NOTE: ProductID is Primary key and also set it to output parameter
for example If material type is 500 and product ID generated 55 and random no generated 5434, then part no become 555-5434
Now my question is how could I store partno in same table, I am somewhat trying like that
Connection.Open()
Dim trn As SqlClient.SqlTransaction
trn = Connection.BeginTransaction
Using trn
Dim sqlcode As New SqlParameter("#ProductID", Products.ProductID)
sqlcode.Direction = ParameterDirection.InputOutput
Using Command As New SqlCommand("Product_Write", Connection, trn)
Command.CommandType = CommandType.StoredProcedure
Command.Parameters.Add(sqlcode) Command.Parameters.AddWithValue("#MaterialType", Materialtype.MaterialTypeCode)
Command.Parameters.AddWithValue("#CategoryID", category.CategoryId)
Command.Parameters.AddWithValue("#ProductName", Products.ProductName)
Command.Parameters.AddWithValue("#ProductDescription", Products.ProductDescription)
Command.Parameters.AddWithValue("#ProductActive", Products.ProductActive)
Command.Parameters.AddWithValue("#ProductImage", Products.ProductImage)
Command.Parameters.AddWithValue("#PartNo", 0)
Command.ExecuteNonQuery()
Products.ProductID = CInt(sqlcode.Value)
'Insert the part no
Dim random As New Random()
Dim value As Integer = random.Next(9999)
Dim PartNo As String = CType((Materialtype.MaterialTypeCode + Products.ProductID).ToString + "-" + value.ToString, String)
'Dont know what to do
trn.Commit()
Connection.Close()
End Using
End Using
End Using
Return Products
End Function
ALTER PROCEDURE [dbo].[Product_Write]
#ProductID bigint OUTPUT,
#MaterialType int,
#CategoryID bigint,
#ProductName VARCHAR(MAX),
#ProductDescription VARCHAR(MAX),
#ProductActive Bit,
#ProductImage VARCHAR(MAX),
#PartNo VARCHAR(30)
AS
IF (#ProductID=0)
BEGIN
INSERT INTO T_Product(
MaterialType,
CategoryID,
ProductName,
ProductDescription,
ProductActive,
ProductImage,
PartNo)
VALUES(
#MaterialType,
#CategoryID,
#ProductName,
#ProductDescription,
#ProductActive,
#ProductImage,
#PartNo)
SET #ProductID=SCOPE_IDENTITY()
END
ELSE
UPDATE T_Product SET
MaterialType=#MaterialType,
CategoryID=#CategoryID,
ProductName=#ProductName,
ProductDescription=#ProductDescription,
ProductActive=#ProductActive,
ProductImage=#ProductImage,
PartNo=#PartNo
WHERE ProductID=#ProductID
Please help me out
Thanks
If you can update the Stored Procedure you would need something along these lines
declare #id int
set #id = SCOPE_IDENTITY()
UPDATE <Table> Set PartNo = MaterialType + convert(varchar(max),#id) + convert(varchar(4),CEILING(9999*RAND())) Where ID = #id

Stored Procedure fails to see parameters or returns a convert data type error

I've run into a rather annoying problem which I cannot seem to get to the root of.
I've searched the internet for similar problems - and I've found a few unanswered in other forums - so I thought I'd give it a go here.
The following WebMethod of a webservice contacts a database, which works fine, and runs a stored procedure.
The problem is, that when I pass the parameters as single characters it tells me it cannot find either of the parameters and when I pass a full length "ean" parameter it tells me the following error message:
System.Data.OleDb.OleDbException: Error converting data type varchar to int.
at System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr)
at System.Data.OleDb.OleDbDataReader.NextResult()
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteReader()
at Service.GetOthersRatings(Int32 personID, String ean)
And now here's the Webmethod:
[WebMethod(Description = "GetRatings")]
public string GetRatings(int personID, string ean)
{
string ratings = "";
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand command = new OleDbCommand("GetRatings", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("#personID",OleDbType.Integer,10).Value = personID;
command.Parameters.Add("#ean",OleDbType.VarChar,30).Value = ean;
try
{
connection.Open();
myReader = command.ExecuteReader();
if (myReader.HasRows)
{
while (myReader.Read())
{
ratings = myReader.GetString(0);
}
}
else
{
ratings = "Null";
}
}
catch (Exception e)
{
ratings = "Error - " + e.ToString();
}
finally
{
}
return ratings;
}
One thing that's worth mentioning is that if I remove the OUTPUT part of the SP, it runs fine down to and returns
ratings = "Null";
But seeing that I try to read a String from the reader, I don't see why it wouldn't work with a varchar output from the SP.
In the Database we have the corresponding Stored Procedure, which works fine if just executed in SQL Server Management Studio:
IF ( OBJECT_ID('GetRatings') IS NOT NULL )
DROP PROCEDURE GetRatings
GO
CREATE PROCEDURE GetRatings
#ratingschars varchar(36) = NULL OUTPUT,
#personID int,
#ean varchar(30)
AS
BEGIN TRAN
SET NOCOUNT ON;
BEGIN
DECLARE #pris varchar(2)
DECLARE #forventet varchar(2)
DECLARE #smag varchar(2)
DECLARE #count varchar(30)
IF EXISTS(SELECT * FROM feedback where personID = #personID AND ean = #ean)
BEGIN
SELECT #pris = (SELECT CAST(pris AS varchar(2)) FROM feedback where personID = #personID AND ean = #ean)
SELECT #forventet = (SELECT CAST(forventet AS varchar(2)) FROM feedback where personID = #personID AND ean = #ean)
SELECT #smag = (SELECT CAST(smag AS varchar(2)) FROM feedback where personID = #personID AND ean = #ean)
SELECT #ratingschars = #pris + #forventet + #smag
END
ELSE
BEGIN
SELECT #pris = (SELECT CAST(avg(pris) AS varchar(2)) FROM feedback WHERE ean = #ean)
SELECT #forventet += (SELECT CAST(avg(forventet) AS varchar(2)) FROM feedback WHERE ean = #ean)
SELECT #smag += (SELECT CAST(avg(smag) AS varchar(2)) FROM feedback WHERE ean = #ean)
SELECT #count += (SELECT CAST(count(*) AS varchar(30)) FROM feedback WHERE ean = #ean)
SELECT #ratingschars = #pris + #forventet + #smag + #count
END
END
COMMIT TRAN
Which I've tried to change to output of to int, with the same error results.
I'm stumped - I need help.
Stored-procedure has three parameters where as in your code, you've added only two parameters. So add a third parameter as OutPut type.
EDIT:
Stored Procedure:
ALTER PROCEDURE SampleProc
#no1 int,
#no2 int,
#result int OUTPUT
AS
set #result=#no1+#no2
RETURN
Code to execute the Stored-procedure:
cmd.CommandText = "SampleProc"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cn
Dim no1 as New OleDbParameter("#no1", OleDbType.Integer)
Dim no2 as New OleDbParameter("#no2", OleDbType.Integer)
Dim resultas New OleDbParameter("#result", OleDbType.Integer)
result.Direction = ParameterDirection.Output
no1.Value = 10
no2.Value = 20
cmd.Parameters.Add(no1)
cmd.Parameters.Add(no2)
cmd.Parameters.Add(result)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
Dim returnResult as Integer
returnResult=CType(result.Value,Integer)
can you try the following?
command.Parameters.AddWithValue("#personID", personID);
command.Parameters.AddWithValue("#ean", ean);
Also, IF EXISTS(...) ... ELSE ... can be replaced with:
IF EXISTS(SELECT * FROM feedback where personID = #personID AND ean = #ean)
BEGIN
SELECT #ratingschars
= CAST(pris AS varchar(2))
+ CAST(forventet AS varchar(2))
+ CAST(smag AS varchar(2))
FROM feedback
WHERE personID = #personID
AND ean = #ean
END
ELSE
BEGIN
SELECT #ratingschars
= CAST(avg(pris) AS varchar(2)) --or STR
+ CAST(avg(forventet) AS varchar(2)) --or STR
+ CAST(avg(smag) AS varchar(2)) --or STR
+ CAST(count(*) AS varchar(11))
FROM feedback
WHERE ean = #ean
END
END
or
SELECT #ratingschars --Question: all these columns (pris, forventet, smag) are NOT NULL ?
= CAST(pris AS varchar(2))
+ CAST(forventet AS varchar(2))
+ CAST(smag AS varchar(2))
FROM feedback
WHERE personID = #personID
AND ean = #ean
IF #ratingschars IS NULL
BEGIN
SELECT #ratingschars
= CAST(avg(pris) AS varchar(2))
+ CAST(avg(forventet) AS varchar(2))
+ CAST(avg(smag) AS varchar(2))
+ CAST(count(*) AS varchar(30))
FROM feedback
WHERE ean = #ean
END

linq query with group

I'm having a table which contains userId, regBy, regDate and so many..
I need a out of regDate, regBy and count(userId).
How can a query this using LINQ..
From what I understand, you want to group by two fields, regDate and regBy. In that case, the select statement looks something like this:
var myQuery = from User myUser in myContext.Users
group myUser by new { regDate = myUser.regDate , regBy = myUser.regBy } into g
select new
{
regDate = g.Key.regDate,
regBy = g.Key.regBy,
Count = g.Count()
};

Resources