[SOLVED] How to query from table's view in .Net C#?

Issue

How to query on table’s view in .Net C# web application ?

for example, here [View_App_Academic] is my table view. My code is listed below. under db scheme, I am not able to see the view due to my user privilege.

string strquery = "select * from [dbo].[View_App_Academic] where recruitment_id=" + 
RecruitDropDownList.Text + " and ref_no='" + RefDropDownList.Text + "'";

SqlCommand objCMD = new SqlCommand(strquery, conn);

Solution

Use parameterized query always.
Remove [dbo] from your query, you don’t need to add [dbo] because it is default database schema.
Change your code to this.

string strquery = "select * from View_App_Academic where [email protected]_id and [email protected]_no";

SqlCommand objCMD = new SqlCommand(strquery, conn);
objCMD.Parameters.AddWithValue("@recruitment_id", RecruitDropDownList.Text);
objCMD.Parameters.AddWithValue("@ref_no",RefDropDownList.Text);
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = objCMD;

DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet);

Hope it helps.

Answered By – Harshil Raval

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *