Monthly Archives: January 2016

T-SQL Update Table From Another Table

Let say you have two tables and you want to update values in first table with values from second table. That could be for example if you have Product table and you have to load new prices and discounts from Import_Prices table.

The T-SQL code to update data could look like this:

UPDATE Product
SET Product.Price = Import_Prices.Price,
    Product.Discount = Import_Prices.Discount
FROM Product INNER JOIN Import_Prices
   ON Product.ID = Import_Prices.ProductID

In this case column ID of table Product corresponds to column ProductID of table Import_Prices .

Be careful to define relationship correctly and eventually filter only needed rows with WHERE clause. UPDATE command could change all table rows if you don’t narrow it correctly, so be sure that you have backup created before using ad-hoc UPDATE query :)

ASP.NET MVC @Html.EditorFor TextArea

@Html.EditorFor creates single line text box for string data by default. That is good for short fields like user name or article title, but not comfortable for longer multi line content.

For long fields, using of textarea tag is more suitable solution. To inform Razor to use multi line textarea instead of single line text box, add this attribute to your model property:

public string LongText { get; set; }

When you use DataType.MultilineText parameter, Razor will render textarea tag instead of single-line text box. That’s good enough in many cases, but sometimes you want more control over width, height and other styles of textarea. You can change text area styles with CSS:

    width: 600px;
    height: 300px;

This CSS will adjust all textarea tags to same styles. If you want different styles for text fields, define CSS class in view in EditorFor method like this:

@Html.EditorFor(model => model.LongText, new { htmlAttributes = new { @class = "long-text-editor" } })

Another option is to use @Html.TextAreaFor instead of @Html.EditorFor. Advantage of this method is that you don’t need to change model class. That could be preferred option if you work with automatically generated model, like when Entity Framework database first is used. Code in view could look like this:

@Html.TextAreaFor(model => model.LongText, 10, 80, new { htmlAttributes = new { @class = "form-control" } })

Please notice there are 4 parameters used for TextAreaFor method. Second and third parameters define rows and columns of textarea. Sample above will create text area of 10 rows and 80 columns. So, now you have textarea without need to change model.

Happy coding!