Select statement in asp.net

15 pts.
Tags:
ASP.NET
SELECT statement
SQL Select
SQL statement
WHERE clause
How can i use ms sql select statement with where clause in asp.net

Answer Wiki

Thanks. We'll let you know when a new response is added.

The best technique is to create a stored procedure and call that stored procedure in a parametrized query from .NET. There are lots of samples of how to do this on the web depending on what language you prefer.

Discuss This Question: 6  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Rick Martinez
    Mr. Denny is right you should use a stored procedure, but if you want to use sql inside your code then I would look into LINQ or ADO.NET there is a link sample below. DataContext qn = new DataContext(); qn.CommandTimeout = 0; var query = from source1 in qn.data(a_oStart, a_oEnd, plan) on sourc1.person_num orderby source1.name select new { col1.name, col2.lastname }; foreach(var encounters in query) { //loop through records }
    585 pointsBadges:
    report
  • Darryn
    Here is a good tutorial for data driven asp sites: http://www.asp.net/data-access/tutorials I would suggest building the data access and business logic layers in the fashion described, then use them in the web page controls.
    765 pointsBadges:
    report
  • MEHRA
    Hi, friends here I m going to explain how to use SQL statement to select data from any table using asp.net . I will describe that by using three tier architecture You first create a new website .and after that you just add two class library Suppose website Name is Demo 1.ClassLibraryDAL [Data Access Layer] 2.ClassLibraryBAL [BusinessAccess Layer] First write Code in --ClassLibraryDAL i am just putting here the code
    
    =====
     First Tier
    using System.Data;
    using System.Data.SqlClient;
    namespace ClassLibraryDAL
    {
        public class PersonDAL
        {
            public DataTable SelectPeron(int Pid)
            {
                DataTable tab = new DataTable();
                string Con = @"Data Source=sureit44;Initial Catalog=MydataBase;Persist Security Info=True;User ID=sitdev;Password=sitdev1";
                using (SqlConnection con = new SqlConnection(Con))
                {
                    string query="Select AutoId,PName,Address,Phone,City From Person Where AutoId="+Pid+"";
                    SqlCommand cmd = new SqlCommand(query, con);
                    con.Open();
                   // cmd.Parameters.AddWithValue("@AutoId", Pid);
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    da.Fill(tab);
                    con.Close();
                }
                return tab;
            }
        }
    }
    ==
    after that Write Code in ClassLibraryBAL
    Second Tier
    
    
    == using System.Data; using ClassLibraryDAL; namespace ClassLibraryBAL { public class PersonBAL { public DataTable SelectPerson(int id) { PersonDAL objPersonDAL = new PersonDAL(); return objPersonDAL.SelectPeron(id); } } } == UI Third Tier In the Default.aspx page of Demo Website Take one Grid view ,One Textbox and Button Just See this Source code for refrence
    
    <form id="form1" runat="server">
        <div>
        <table>
        <tr>
        <td>
        Enter PersonId 
        </td>
        <td>
        <asp:TextBox runat="server"  ID="txtPersonId"></asp:TextBox>
        </td>
          </tr>
        <tr> 
        <td colspan="2">  </td>
        <td> 
        <asp:Button ID="btnSearch" Tex="SearchPrson" Text="Search" runat="server" 
                onclick="btnSearch_Click"  /></td>
        </tr>
        <tr>
        <td colspan="2">  </td>
        <td> 
        <asp:GridView ID="personGrid" runat="server" ></asp:GridView></td>
        </tr>
      
        
        </table>
        </div>
        </form>
     Code Bihind file of Default.aspx.cs
    
    
    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ClassLibraryBAL; using System.Data; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private void BindGrid() { int personId =int.Parse(txtPersonId.Text.ToString()); DataTable table = new DataTable(); PersonBAL objPersonBAL = new PersonBAL(); table= objPersonBAL.SelectPerson(personId); personGrid.DataSource = table; personGrid.DataBind(); } protected void btnSearch_Click(object sender, EventArgs e) { BindGrid(); } } Now What you can do just Enter Person id in TextBox and Enter Click On Search button I have use here Person Table friend just add reference in the following sequence From ClassLibraryDAL into ClassLibraryBAL and then ClassLibraryBAL into your Default .aspx.cs I Hope This Really helpful for you if you have Any query regrading this article the please let me know at my Id -krajmehra@gmail,com
    145 pointsBadges:
    report
  • carlosdl
    Thanks for shaing this MEHRA. I have an observation about this part:
    Select AutoId,PName,Address,Phone,City From Person Where AutoId="+Pid+”
    Constructing queries that way is a very bad practice, as it makes your site/application vulnerable to SQL injection attacks. You should parameterize your queries instead.
    68,780 pointsBadges:
    report
  • MEHRA
    Ok Carlosdl thank you for feedback from next time I will use Parametrized query
    145 pointsBadges:
    report
  • Pioneerinaspnet
    [...] Select statement in asp.net [...]
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following