Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have multiple sqlquery in page..so use sqlcommand object to fire query...so if i use cmd multiple times then it gives error that cmd already exist so i used cmd1,cmd2,cmd3 etc but it is not good way of coding so how can initialise sqlcommand object publically in top of page????
plz help me...
my code is below:-


C#
string mail = "select CONTACT_EMAIL from QAFranchise.HDR_CONTACT_LIST where CONTACT_EMAIL='"+Class1.MailID+"'";
        SqlCommand cmd5 = new SqlCommand(mail, cn);
        string mailid = Convert.ToString(cmd5.ExecuteScalar());

        string contactID = "select CONTACT_LIST_ID,CONTACT_EMAIL from QAFranchise.HDR_CONTACT_LIST where CONTACT_EMAIL='"+mailid+"' ";
        SqlCommand cmd6 = new SqlCommand(contactID, cn);
        Class1.ContactlistID = Convert.ToInt32(cmd6.ExecuteScalar());


thanks in advance.

[edit]code block added[/edit]
Posted
Updated 17-Nov-12 1:30am
v2

Hi
It is better and easy to use a separate class for the database operations. For Example.

C#
protected void Page_Load(object sender, EventArgs e)
   {
       dbConnection connection = new dbConnection();
       private void FetchContactMail()
       {
          string contactEmail = string.Empty;
          object myData = connection.FetchMyData("select CONTACT_EMAIL from QAFranchise.HDR_CONTACT_LIST where CONTACT_EMAIL = 'yourmail'");
          if (myData != null)
              contactEmail = myData.ToString();
       }
       private void FetchContactID()
       {
           int contactID = -1;
           object myData = connection.FetchMyData("select CONTACT_LIST_ID from QAFranchise.HDR_CONTACT_LIST where CONTACT_EMAIL = 'yourmail'");
           if (myData != null)
               contactID = Convert.ToInt32(myData);
       }
   }


And a new class for database operations.

C#
class dbConnection
{
    private string _connectionString = "your connection string";
    public object FetchMyData(string query)
    {
        object objVal;
        using (SqlConnection con = new SqlConnection(_connectionString))
        {
            con.Open();

            using(SqlCommand cmd = new SqlCommand(query,con))
            {
                objVal = cmd.ExecuteScalar();
            }
        }
        return objVal;

    }

}


Then it will be easier for you to manage.
 
Share this answer
 
Comments
Dominic Abraham 18-Nov-12 1:30am    
If the solution is helpful, please mark it as answer.
Code like this

SqlCommand cmd=new SqlCommand();

cmd.CommandText="";
cmd.Connection=cn;
cmd.ExecuteScalar();

If you have parameters that clear them.
cmd.Parameters.Clear();

cmd.CommandText="";
cmd.executeScalar();

Ask me if you have any questions?
 
Share this answer
 

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