Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi,

I have an ASP.NET website, that has many textboxes to be filled and to be written into SQL. I also have a WinForm application for this.

When I write more than 256 characters into a textbox in winform and INSERT or UPDATE it in SQL column (NVARCHAR(MAX)), have no problem, works fine.

When I try to do the same on my ASP net site, it gives me an error:

Must declare the scalar variable "@"

(yes, there is no variable name!). I don't use any predefinied sql connection string in ASP, i use only the old school method: 1 string with the query, one ExecuteNonQuery() method calling. I always use this way, and works fine in other cases.

I searched many things on google, but no success. I guess it is some IIS limitations or some HTTP limitation?!...

Can somebody tell me a simple solution to be able to upload and long text longer than 256 chars?

Many thanks
Posted
Comments
Wendelius 28-Sep-15 14:18pm    
Even though you posted that you have solution I believe it would make sense to post the code how you build and execute the SQL statement. This way we could check if there's something to correct with code.
DoomMaker79 29-Sep-15 1:57am    
you're right, there's my solution detailed

Mika, you're right, so here's my solution:

before I solved it, I used a class for SQL inserts and updates. I passed 2 args to that class: a string with the values separating them with letter ß, and the name of the database, something like this: TableData.WriteID(columnString, db)

the columnString had this kind of string: "data1ßdata2ßdata3"

then the class splitted the incoming string (Split('ß')) into a string array and then I inserted/updated the sql (using NameValueCollection). This was not working. I got the sample from Mohammed Hadi. It's a really cool stuff.

Then I got back to the reeeeeal old school version:

C#
string sql = "INSERT INTO Table (ID,username,comment,date) VALUES (@id,@username,@comment,@date)";
SqlCommand cmd = new SqlCommand(sql, myConnection);//pre-definied myConnection
cmd.Parameters.Add("@id", id.Text);
cmd.Parameters.Add("@username", username.Text);
cmd.Parameters.Add("@comment", comment.Text);
cmd.Parameters.Add("@date", date.Text);
cmd.ExecuteNonQuery();


And this one's working!
 
Share this answer
 
Comments
Wendelius 29-Sep-15 2:42am    
As far as I can see the solution you tried has several places where things can go wrong. The first thing is that it doesn't use parameterization so strings may end too early depending on the data and problems with type conversions may occur.

Another thing is that depending how the WHERE clause is built you may quite easily end up with syntactical problems.

Query builders are fine in many situations but if you really don't need them I wouldn't use them. Many times simple, fixed operations work much better and are more easily maintained.
argh. I have a solution, but don't know why it works. so I have to use the cmd.parameters.add command in my c# code.

I used a class for sql writing, where I sent the whole string with comma separating, and the class got it into its string[]... nevermind. i thought i was old school, but not... :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900