---
title: "how to validate validations whether textbox is empty or not. using linq to sql. in mvc"  
description: "how to validate validations whether textbox is empty or not. using linq to sql. in mvc"  
author: "venkat gosetty"  
published: 2011-11-18  
updated: 2011-11-22  
canonical: https://www.mindstick.com/forum/357/how-to-validate-validations-whether-textbox-is-empty-or-not-using-linq-to-sql-in-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# how to validate validations whether textbox is empty or not. using linq to sql. in mvc

hi,\
how to [validate](https://www.mindstick.com/forum/1490/what-is-better-solution-for-validate-form-textboxs-value) validations whether [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview) is empty or not. using [linq to sql](https://www.mindstick.com/articles/338528/how-to-use-linq-to-sql-select-query-using-c-sharp).\
\
Regards,\
Venkat

## Replies

### Reply by Chris Anderson

hi,\
\
you can put validation using linq to sql query something like below:\
\

```
DataContext data = new DataContext();string value="S001";var query = from p in data.Persons                   where p.Id ==
value                                  select p;
```

if (query.Count() > 0) throw new Exception("Id already exists in the database"); \

### Reply by venkat gosetty

Actually i have taken model class .dbml (linq to sql template)\

### Reply by Chris Anderson

hi venkat,\
\
if you want validate your textbox, simply use:\
\
namespace:\
using System.ComponentModel.DataAnnotations;\
\
and above the property add following:\
[StringLength(50),Required] \
public string Name { get; set; } \
\
why you use [linq](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries) to sql for validation?\


---

Original Source: https://www.mindstick.com/forum/357/how-to-validate-validations-whether-textbox-is-empty-or-not-using-linq-to-sql-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
