By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 1,029 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

Error: insert records into SQL database

P: 1
i am trying to insert records into SQL database ..........here is the code snippet
I receive the System.Data.sqlclient..... Exception

Code:
Expand|Select|Wrap|Line Numbers
  1. public void setData(CustInfo c)
  2.         {
  3.             CQuery cq = new CQuery();
  4.             ExecuteQuery1(CQuery.custQuery,c);
  5.         }
  6.  
  7.  
  8.         public void ExecuteQuery1(string query,CustInfo c)
  9.         {
  10.             try
  11.             {
  12.                 string conn = "Persist Security Info =false;Server=intel/sqlexpress;Initial Catalog = Customer;Integrated Security = true";
  13.  
  14.                 using (SqlConnection sqlcon = new SqlConnection(conn))
  15.                 {
  16.  
  17.                     using (SqlCommand cmd = new SqlCommand(query, sqlcon))
  18.                     {
  19.  
  20.                         cmd.Parameters.Add(new SqlParameter(@Firstname, c.Firstname));
  21.                         cmd.Parameters.Add(new SqlParameter(@Email, c.Email));
  22.                         sqlcon.Open();
  23.                         cmd.ExecuteNonQuery();
  24.                     }
  25.                 }
  26.             }
  27.             catch (SqlException se)
  28.             {
  29.  
  30.                 Console.WriteLine(se.ToString());
  31.             }
  32.         }
  33.     }
  34. }
  35.  
Oct 6 '09 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 750
PRR
MSDN
Expand|Select|Wrap|Line Numbers
  1.  
  2. private static void CreateCommand(string queryString,
  3.     string connectionString)
  4. {
  5.     using (SqlConnection connection = new SqlConnection(
  6.                connectionString))
  7.     {
  8.         SqlCommand command = new SqlCommand(queryString, connection);
  9.         command.Connection.Open();
  10.         command.ExecuteNonQuery();
  11.     }
  12. }
  13.  
1. Check whether your connection string is correct.
2. Go to query analyzer and run your sample query.
Oct 6 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.