I declared my connection string in webconfig like this , <configuration>
<connectionStrings>
<add name="Dbconnection"
connectionString="Server=USER-PC\SHUKLAJI; Database=OnlineExamination; Integrated Security=True; pooling=false; "
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
and then I am using like this ,
public class DALRegister
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Dbconnection"].ToString());
public int UserDetails(StudentRegistration sr)
{
try
{
SqlCommand cmd = new SqlCommand("prcregisteration", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", sr.Name);
cmd.Parameters.AddWithValue("@username", sr.UserName);
cmd.Parameters.AddWithValue("@password", sr.Password);
cmd.Parameters.AddWithValue("@email", sr.Email);
cmd.Parameters.AddWithValue("@phone", sr.Phone);
con.Open();
int result = cmd.ExecuteNonQuery();
cmd.Dispose();
return result;
}
catch(Exception ex)
{
throw;
}
}
}
Is it not the best way to use Dbconnection? how to use it, in the best possible manner ? please reply soon. Thanks