Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:57:48+00:00 2026-06-15T19:57:48+00:00

I am trying to write a view and retrieve data from that view, I

  • 0

I am trying to write a view and retrieve data from that view, I have done this :

 string command = "if OBJECT_ID('try1') IS NOT NULL Drop view try1" +"\n"+"go"+"\n";
        command = command + "Create view try1 as select WBSCode,Description,TerritoryCode,AmountReleased,convert(varchar(25),CreatedOn,106) as CreatedOn,IsEnable from WBS where WBSCode like '%" + msrch + "%'";
        command = command +"\n"+"go"+"\n"+"select WBSCode,Description,TerritoryCode,AmountReleased,convert(varchar(25),CreatedOn,106) as CreatedOn,IsEnable from try1 where TerritoryCode in (select TerritoryCode from Territory where StateCode='" + mbcode + "')";
        SqlCommand cmdd = new SqlCommand(command);
        cmdd.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter(cmdd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            mngwbsGV1.DataSource = dt;
            mngwbsGV1.DataBind();
        } 

but the issue is i am getting an error in da.Fill(dt) that:

Incorrect syntax near ‘go’.
‘CREATE VIEW’ must be the first statement in a query batch.
Incorrect syntax near ‘go’.

But when I am running this in my SQL Server 2008 it’s running but not retrieving the data

What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:57:49+00:00Added an answer on June 15, 2026 at 7:57 pm

    You should be creating your view separately, e.g. during installation or version upgrade – using regular SQL scripts.

    If you must do this in code – you need to do it this way:

    using(SqlConnection conn = new SqlConnection(--your connection string here--))
    {
        SqlCommand dropView = new SqlCommand("IF OBJECT_ID('try1') IS NOT NULL DROP VIEW try1", conn);
    
        conn.Open();
    
        dropView.ExecuteNonQuery();
    
        SqlCommand createView = new SqlCommand("CREATE VIEW try1 AS SELECT WBSCode, Description, TerritoryCode, AmountReleased, CONVERT(VARCHAR(25), CreatedOn, 106) AS CreatedOn, IsEnable FROM WBS ", conn);
    
        createView.ExecuteNonQuery();
    
        conn.Close();
    }
    

    and then you can select from it using this (use a parametrized query instead of concatenating together your SQL command to avoid SQL injection attacks and to improve performance!):

    SqlCommand selectCmd = new SqlCommand("SELECT WBSCode, Description, TerritoryCode, AmountReleased, CreatedOn, IsEnable FROM try1 WHERE TerritoryCode IN (SELECT TerritoryCode FROM Territory WHERE WBSCode LIKE @msrch AND StateCode = @mbcode", conn);
    
    selectCmd.Parameters.Add("@msrch", SqlDbType.VarChar, 50).Value = "%" + msrch + "%";
    selectCmd.Parameters.Add("@mbcode", SqlDbType.VarChar, 50).Value = mbcode;
    
    SqlDataAdapter da = new SqlDataAdapter(selectCmd);
    
    DataTable dt = new DataTable();
    da.Fill(dt);
    
    if (dt.Rows.Count > 0)
    {
        mngwbsGV1.DataSource = dt;
        mngwbsGV1.DataBind();
    } 
    

    But then again: if you have such a simple select – what do you need a view for in the first place, especially if you just want to create it, select from it, and drop it again right away. Doesn’t make any sense at all ….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a database view that selects from 2 tables. Here
I am trying to write a url.py where I have a simple view for
I'm trying to write my first simple mvc app. I have a Main View
I am trying write a function that generates simulated data but if the simulated
I am trying to write a view that will generate a report which displays
I am trying to write a simple view that renders a single field that
I am trying to write a simple table view editor for a Core Data
I have been trying to write a delegate to a view controller which has
I am trying to write a view/SQLAlchemy query that will allow me to display
I am trying to write a view helper but it's not returning the correct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.