Click here to Skip to main content
Rate this: bad
good
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 0:43am
Edited 17-Nov-12 1:30am
Nelek47.1K
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 at 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
0 OriginalGriff 278
1 Maciej Los 265
2 Nguyen.H.H.Dang 230
3 Peter Leow 195
4 arvind mepani 187


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