Error: insert records into SQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sheetal shah
    New Member
    • Oct 2009
    • 1

    #1

    Error: insert records into SQL database

    i am trying to insert records into SQL database ..........here is the code snippet
    I receive the System.Data.sql client..... Exception

    Code:
    Code:
    public void setData(CustInfo c)
            {
                CQuery cq = new CQuery();
                ExecuteQuery1(CQuery.custQuery,c);
            }
    
    
            public void ExecuteQuery1(string query,CustInfo c)
            {
                try
                {
                    string conn = "Persist Security Info =false;Server=intel/sqlexpress;Initial Catalog = Customer;Integrated Security = true";
    
                    using (SqlConnection sqlcon = new SqlConnection(conn))
                    {
    
                        using (SqlCommand cmd = new SqlCommand(query, sqlcon))
                        {
               
                            cmd.Parameters.Add(new SqlParameter(@Firstname, c.Firstname));
                            cmd.Parameters.Add(new SqlParameter(@Email, c.Email));
                            sqlcon.Open();
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
                catch (SqlException se)
                {
    
                    Console.WriteLine(se.ToString());
                }
            }
        }
    }
    Last edited by PRR; Oct 6 '09, 10:46 AM. Reason: Please post code in [code] [/code] tags.
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    MSDN
    Code:
    private static void CreateCommand(string queryString,
        string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(
                   connectionString))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
    }
    1. Check whether your connection string is correct.
    2. Go to query analyzer and run your sample query.

    Comment

    Working...