Passing a string with \r to sql stored proc in c# - c#

I'm populating a model in an mvc controller, the model has a public string of Name which is to capture the AD name of the users using the web app.
in the controller in debug model.Name has the right value. So something like DOMAIN\rjones.
the problem is if a user has an initial of r or n then the \r or \n (maybe other chars) are getting stripped out.
I'm passing the values to sql by the following:
db.Database.ExecuteSqlCommand("EXEC dev.uspCreateNewDevCase #LoggedBy, #LoggedFor, #ShortDescription, #LongDescription, #DateDue"
, new SqlParameter("#LoggedBy", model.Name )
, new SqlParameter("#LoggedFor", model.LoggedFor)
, new SqlParameter("#ShortDescription", model.ShortDescription)
, new SqlParameter("#LongDescription", model.LongDescription)
, new SqlParameter("#DateDue", model.DateDue)
in debug, the model.Name , new SqlParameter("#LoggedBy", model.Name ) has the correct string text.
I have stripped the stored procedure down in sql to just write that value to a table, and when I query the table it has the value of DOMAIN jones the \r has been taken out.
As mentioned this is dynamic as in it will depend on the user using the app if they have an r or n in the name. Is there a way to encode this or preserve it so when it passed to SQL it maintains the \r or \n.
I'm assuming it is being lost in C# as in T-SQL you can store that value in a variable and insert it into a table. The T-sql data type is NVARCHAR
fixed
I was passing the value to the controller by an Ajax method of:
$.ajax({
type: "POST",
url: urlAction,
data: JSON.stringify({
Name: #HttpContext.Current.User.Identity.Name ,
However, I have set the model value in the view via #Html.HiddenFor to be $.ajax({
type: "POST",
url: urlAction,
data: JSON.stringify({
Name: #HttpContext.Current.User.Identity.Name , which passes the value with a \ in it and then writes correctly to the SQL table.

If you set a variable this way:
string myString="This is\rmy text"; -> "This is" + (char)13 + "my text"
It will be stored with a replace of \r as (char)13 (that means the ASCII code CR for Carriage Return)
If you double the \ it won't
string myString="This is\\rmy text"; -> "This is\rmy text"
Another general way is to start the string with the "at"-sign:
string myString=#"This is\rmy text"; -> "This is\rmy text"
Are you sure, that the variable contains the right value before you pass it?
EDIT:
What you see might not be what is there. Look at this:
The preview on mouse hover shows the \r sign, but if you click the lense you get:
So: please check again, if you really have what you think that you have...

Related

String Interpolation from URL in the database

I have a table in my database that keeps the route URL that I need to call.
For example I have this URL:
http://localhost/Account/Get/{id}
When I retrieve this value from the database, is coming into a variable. How can I do string interpolation to replace the {id} with another variable that I have with the value?

String contents change while pasing from view to controller Get method

In MVC application, I am passing string taskName = "a#&+" from UI text box.
In the controller, parameter taskName changes to taskName = "a".
On debugging the view, the value of taskName is displayed as expected.
But while passing from view to controller, it changes unexpectedly.
Post method is then posting same incorrect string back to UI. How to obtain taskName = "a#&+" in the controller GET method?
I am new to MVC. Please let me know, if I can provide further relevant information.
I am not exactly sure how you are passing data from a text box on the page to the controller parameter but based on your description it appears that you must be using javascript to get the value of the text box and then attaching it to the url. In this case you need to encode the value of the of the text box before using it by using the javascript encodeURIComponent function.
See this jsfiddle for an example: http://jsfiddle.net/a7ek6whn/
The characters # and & are reserved characters in a query string, this is why your value is truncated. You need to UrlEncode your values in order to get the correct string in the Get, before you perform your Get request.

How to get key value from query string from SharePoint?

I have an url: http://test?ID=i:0#.w|pro\administrator
I would like to get the current user (from Sharepoint) with c# code.
So I used the QueryString, here is:
private string userLogin = HttpContext.Current.Request.QueryString["ID"];
But the problem is that the value returned by QueryString is i:0.
Why?
Because that's what your URL has specified the value of the ID parameter is. # is a special character in URLs and everything after it is a separate part of the URL. You should URL encode the value of the parameter when constructing the URL in the first place to ensure that # (and any other characters) are treated as characters in value of the ID query parameter.
If you need to get current user, why do not use SP native method:
SPWeb web = SPControl.GetContextWeb(SPContext.Current);
uName = web.CurrentUser.ID;

place database string value with \r\n into wpf textbox

i'm creating a small application with .edmx for database connection.
inside my database i created a string with example value:
this is:\r\nA new line
when I call this value (in debug) i see the value changed to
this is:\\r\\nA new line
resulting in a wpf textbox showing
this is:\r\nA new line
instead of
this is:
A new line
Any thoughts over what i'm doing wrong here?
EDIT:
Windows 8.1
VS2013 Express
SQL Server Express (LocalDb)
The values are stored in the database via a t-SQL command. (I'm still in the startup of the project)
Insert into Atable
(ID, value)
VALUES
(1, 'this is:\r\nA new line')
Because in sql \r does not mean anything special. In c#, \r is the same as ascii character #10, which is unprintable. What you are trying to make is an "escape sequence", which only exist in c#.
If you create the escaped character in c# and save it to sql, it will read back just fine. But if you type the escape character in sql, it won't be what you expect.
So, in an sql query, you would do this:
select 'this is:' + CHAR(13) + CHAR(10) + 'A new line' -- same as "\n\r" in c#
In c#, it would be
string message = "this is\r\nAnew line"
Or, better, use the built in
string message = "this is" + Environment.NewLine + "A new line";

Strange issue about double numbers with commas

I have a field in db named 'loss' which is of type float.
Here is how I save it in db:
[HttpPost]
public int SaveLoss(float loss)
{
var t = (from x in db.tblTest
where x.id == 8
select x).First();
t.loss = loss; //also tried t.loss = loss / 1;
db.SaveChanges(); //entity framework
}
This is how I always work and in all the other tables it works fine.
I enter the loss via a textbox and here is the Ajax call:
$.ajax({
type: "POST",
url: "/Test/SaveLoss",
data: {
loss: parseFloat($('#myTextbox').val().replace(',', '.'))
}
....
If in the textbox I enter 66.55, everything works great. If in the textbox I enter 66,55, the value stored in db is 6655. This issue doesn't happen on my computer. When I publish the application on server, it occurs. Why is this annoying issue happening and how to fix it?
Fiddler says that 66.55 is input parameter of SaveLoss.
It looks like either the browser or server has a different number format, perhaps it is running in a culture where ',' is used as a decimal separator and '.' as a thousands separator.
You could try setting Thread.CurrentThread.CurrentCulture to CultureInfo.InvariantCulture after starting the server or before running the database query.
If You have generated EF mapping using database first. You should be concerned about EntityFramework..
EF maps SQL column type float into C# type double not float
see: SqlClient for Entity FrameworkTypes
Pass it as string and try like this while saving in entity framework:
var amount = decimal.Parse(InvoiceAmount, NumberStyles.Any);

Categories

Resources