15 pts.
 Select statement in asp.net
How can i use ms sql select statement with where clause in asp.net

Software/Hardware used:
ASKED: July 18, 2010  5:17 AM
UPDATED: March 5, 2011  12:38 PM

Answer Wiki:
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.
Last Wiki Answer Submitted:  July 18, 2010  9:17 pm  by  Denny Cherry   64,520 pts.
All Answer Wiki Contributors:  Denny Cherry   64,520 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

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 pts.

 

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 pts.

 

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 pts.

 

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.

 63,535 pts.

 

Ok Carlosdl thank you for feedback from next time I will use Parametrized query

 145 pts.