Click here to Skip to main content
15,067,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Plz help how to optimise this code so that it will use less server resourses
and is it using so huch server resourses that my serving application pool is requesting recycle?? is it possible that this code will use more than 500mb of virtual memeory


try
{
int flg = 0;
SqlConnection cn = new SqlConnection("Data Source=servername;Initial Catalog=enefinde_memb;Persist Security Info=True;User ID=enefinde_admin;Password=abc");
String sql = "select email from regmember where email='" + txtEmail.Text + "'";
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lblExists.Text="User name already exist Please Try Another One";
flg = 0;
}
else
{
flg = 1;
}

dr.Close();

if (flg == 1)
{
sql = "insert into regmember(enename,email,password,keyquestion,answer,address,city,state,country,perphone,mobile,cname,activity,yearofstarting,noofemp,location,website,phone)values('" + txtFname.Text + " " + txtMname.Text + " " + txtLname.Text + "','" + txtEmail.Text + "','" + txtPass.Text + "','" + ddlQuestion.Text + "','" + txtAns.Text + "','" + txtAdd.Text + "','" + txtCity.Text + "','" + txtState.Text + "','" + ddlCountry.Text + "','" + txtPerphone.Text + "','" + txtMob.Text + "','" + txtCname.Text + "','" + ddlAct.Text + "','" + txtStart.Text + "','" + ddlNoemp.Text + "','" + ddlCcountry.Text + " " + txtCstate.Text + " " + txtCcity.Text + "','" + txtWebsite.Text + "','" + txtPhone.Text + "' )";
cmd.CommandText = sql;
cmd.Connection = cn;
int i = cmd.ExecuteNonQuery();
if (i > 0)
//Response.Write("Record Inserted");
Response.Redirect("~/html/thankyou.html");
else
Response.Write("Not Inserted");
}

cn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Posted
Updated 18-Jan-10 23:34pm
v2

You could change your code into
String sql = "select count(email) from regmember where email='" + txtEmail.Text + "'";
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
if (int.Parse(cmd.ExecuteScalar().ToString()) == 0)
{
//user not exists
}
else
{
//user exists
}


BTW read about command parameters here[^]
   
you should use layers architecture like 3 tier, N tier. So that you can divide the resources so that load will be balanced.

The architecture contains like DataLayer, BusinessLogicLayer, ApplicationLayer. try search engines with these keyword.

The great suggestion your web page should not contain any db related code. Also the architecture defined the same
   
Reasons I'd fire anyone who wrote this code for me

1 - hard coded connection string
2 - SQL code in presentation layer
3 - lack of any sort of database security, esp in what looks like an account creation page, which anyone could presumably access and thus erase or hack the entire DB
4 - the password for the database is abc
5 - using an integer for a boolean flag
6 - this method does several things, which should be refactored into different methods
7 - using Response.Write to communicate with the user instead of setting text in a properly styled and positioned control

Just to add, n-tiered development does not balance any sort of load, that's a ludicrous claim. It creates maintainable, readable code that stands less chance of having parts that are redundant.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900