0

I am using inline query in asp.net but problem is i dont now how to use N suffix properly pls guide....

  cn.Open();
    SqlCommand cmd = new SqlCommand("insert into newsactivity VALUES(@newstitle, @newsdetails ,@dated,@type)",cn);
    cmd.Parameters.AddWithValue("@newstitle",txtnewstitle.Text);
    cmd.Parameters.AddWithValue("@newsdetails",N'txtnewsdetails.Text');
1
  • When you are using parameters then setting SqlDbType to NVarChar should be good enough Commented Jun 18, 2012 at 5:55

3 Answers 3

5

It should be enough to say

cmd.Parameters.AddWithValue("@newsdetails", txtnewsdetails.Text);

EDIT: Should that fail, then this should work:

cmd.Parameters.Add("@newsdetails", SqlDbType.NVarChar, 1024).Value = txtnewsdetails.Text;
Sign up to request clarification or add additional context in comments.

Comments

4

You don't need to. .NET strings are already unicode. N is just tells Query Analyzer that the string literal after the N is Unicode.

e.g.

cmd.Parameters.AddWithValue("@newsdetails","मैं stackoverflow प्यार है");

Just make sure that the field newsdetails in your newsactivity table is NVARCHAR(..)

Edit That's strange. Possibly AddWithValue hasn't inferred the type of the parameter correctly. You can specify it explicitly:

SqlParameter param = cmd.Parameters.AddWithValue("@newsdetails",txtnewsdetails.Text);
param.SqlDbType = SqlDbType.NVarChar;

Another good reason to specify the type and length explicitly is if you are using the parameter in a WHERE clause filter. See AddWithValue without DBType causing queries to run slowly

1 Comment

its nvarchar already ... and its a freetextbox but after saving it displays ????? chars....how ever sql server accepts unicode when i insert it directly in sql server
0

use SqlDbType.NVarChar in your parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.