Insert data into database based on condition in MVC using Linqto sql [Resolved]

Posted by Mandlaa under ASP.NET MVC on 9/17/2013 | Points: 10 | Views : 6145 | Status : [Member] | Replies : 6
This is my table
CREATE TABLE [dbo].[ProductsHondaofConyers](
[TagID] [int] NOT NULL,
[Sno] [nvarchar](max) NULL,
[Name] [nvarchar](max) NULL,
[Description] [nvarchar](max) NULL,
[Price] [nvarchar](max) NULL,
[Image] [nvarchar](max) NULL,
CONSTRAINT [PrimaryKey_d545e66e-4adb-4a3c-82dd-97220799da044] PRIMARY KEY CLUSTERED
(
[TagID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO
At the time of inserting data into database i want to entered value(TagId) already existing into database it show error message not exist store that data in to databasehow in MVC?
Controller:

[HttpPost]
public ActionResult Create(ProductsHondaofConyer p)
{
ProducdataDataContext db = new ProducdataDataContext();
db.ProductsHondaofConyers.InsertOnSubmit(p);
db.SubmitChanges();
return RedirectToAction("Index");
}
this is my controller code




Responses

Posted by: Jayakumars on: 9/17/2013 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
hi
mandlaa

try this code working good here otherwise post your code i ill check and send you

[HttpPost]
public ActionResult Create(FormCollection collection,ProductsHondaofConyer p)
{
try
{
var Tagid = collection["TagID"];
DataClasses1DataContext db = new DataClasses1DataContext();
var qry = (from c in db.ProductsHondaofConyers where c.TagID == Convert.ToInt32(Tagid) select c).Count();
if (qry !=1)
{
db.ProductsHondaofConyers.InsertOnSubmit(p);
db.SubmitChanges();
}
// TODO: Add insert logic here

return RedirectToAction("Index");
}
catch
{
return View();
}
}



Mark as Answer if its helpful to you

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 9/17/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.c-sharpcorner.com/UploadFile/3d39b4/simple-select-insert-update-and-delete-using-linq-to-sql/

If this post helps you mark it as answer
Thanks

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 9/17/2013 [Member] Starter | Points: 25

Up
0
Down
Thank you for replying,
I want to check condition entered value exit in database display error message,not existed entered value insert data into database


Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Alternate approach to check for records existence using LINQ is as follows:
var Tagid = collection["TagID"];

var checkExisting = from c in db.ProductsHondaofConyers
where c.TagID == Convert.ToInt32(Tagid)
select c;

if (checkExisting.FirstOrDefault() != null)
{
// code for insert
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 9/18/2013 [Member] Starter | Points: 25

Up
0
Down
It's working fin,At the time of enter already existing value show error message how to do this in MVC?

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Mark it as answer....

Refer this for data validation
http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation
http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-1

Simply you can use TRY...CATCH blocks to catch the raised error...
try 

{
var Tagid = collection["TagID"];
var checkExisting = from c in db.ProductsHondaofConyers
where c.TagID == Convert.ToInt32(Tagid)
select c;

if (checkExisting.FirstOrDefault() == null)
{
// code for insert
}
}
catch
{
//code to display error message
}




Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response