Click here to Skip to main content
12,690,575 members (32,269 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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:-


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 17-Nov-12 1:43am
Updated 17-Nov-12 2:30am
Nelek112.3K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

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.

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.
  Permalink  
Comments
Dominic Abraham 18-Nov-12 1:30am
   
If the solution is helpful, please mark it as answer.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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?
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170117.1 | Last Updated 17 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100