In c how would I search a database for A A specific city B C
In c#, how would I search a database for:
A) A specific city
B) Cities that start with a certain letter
Population of 10 Show All Population 145786 87643 48190 52281 125872 173372 47707 City Total Population (all cities) Kansas Lawerence Lexena Manhatten Olathe Overland Park Salina Average Population (all cities) City of highest population City of lowest population O Max Population Min Population C Exact Match Starts With Show by Population Search by city nameSolution
suppose your text box name is Text1 and the button name is Search1
Based on radio button checked you have to submit the query to sql.
Let the radio button names are r1 and r2.
This is code related to connection with SQL Server
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(\"Data Source=OM-PC;Initial Catalog=aspexample;User ID=sa;Password=tiger1;\");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void search1_Click(object sender, EventArgs e)
{
string city = \"\";
if (r1.Checked == true)
{
cityname=text1.Text;
SqlCommand cmd = new SqlCommand(\"Select * from table where city like cityname \"\')\", con);
}
if (r2.Checked == true)
{
cityname=charAt(text1.Text,0);//Retrive the character at first position
SqlCommand cmd = new SqlCommand(\"Select * from table where city like cityname+\'%\' \"\')\", con);
}
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
refress();
}
catch (Exception ex)
{
Literal1.Text = ex.Message;
}
}
You are advised to change your SqlConnection string where
Data Source = Server Name,
Initial Catalog = Database Name

