Click here to Skip to main content
15,918,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys, i am using visual studio 2010 c#. This function is suposed to connect to a database and create a view for further use. Unfortunately when i run the program it says "Incorrect syntax near 'View' ". But the syntax is correct! I try running the string comand in SQL query and it works. But form Visual c# sends the error message. I have doublechecked that there is NOT another view in the database. And if i take off the "CREATE VIEW" part from the comand string and leave the rest, the program works! Thats weird... Plz HELP. Thanks in avdvance.

 private void button1_Click(object sender, RoutedEventArgs e)

     int i = 0;
     Boolean Error = false;
String comand=null;
int j = i - 1;

String connect = @"Data Source=STAGE1-SW\SQLEXPRESS;Initial Catalog=License_Data;Integrated Security=True";
     SqlConnection con = new SqlConnection(connect);
     SqlDataReader rdr = null;
     String comand2 = null;
         comand = @"CREATE VIEW [Vista0] AS (SELECT * FROM [Customers] WHERE (Ragione_Sociale LIKE (@Nome + '%')))"; /*This is the part that is run ignore the other command for now*/
         comand = @"Create View Vista" + i.ToString() + " as SELECT  * FROM Vista" + j.ToString() + " WHERE (Ragione_Sociale LIKE @Nome + '%')";


     SqlCommand cmd = new SqlCommand(comand, con);
     cmd.Parameters.Add("@Nome", System.Data.SqlDbType.VarChar).Value ="ma";




in SQL you cannot have views with parameters[^]

I suggest you creating a Stored Procedure and than you can pass the parameter value.

Share this answer
Elman90 21-Oct-11 11:31am    
Thanks! Havent tried but that must be it.
Best Regards man ;)
SqlConnection conn = null;
conn = new SqlConnection("yourConnectionString");
string strSQLCommand = "CREATE VIEW vw_YourView AS SELECT YOurColumn FROM YourTable";
SqlCommand command = new SqlCommand(strSQLCommand, conn); 
string returnvalue = (string)command.ExecuteScalar(); 
Share this answer
Elman90 21-Oct-11 11:22am    
Thaknks for the reply, but thats basically what i wrote, (the only diff is ExecuteScalar() instead of ExecuteNonQuery() ) Still i tried that too to be sure, Doesnt work. BTW its a WPF application,, if that makes a difference
theanil 21-Oct-11 11:31am    
try running exact querry, i tried it is working

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