Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi experts,
 
I am Using Microsoft Visual Studio 2005 and MSSQL Server 2005.
 
In my Website i am using ODBC Connection using "System DSN".
In my code of get Data, it is working successfully.
 
But, in my code of Insert Data to Database,
I am Execution Non Query which is not taking effect in database.
 
My Function is as under.
 
public int Savedata(OdbcParameter[] p)
    {
        int i = 0;
        con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString());
        cmd = new OdbcCommand();
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Save_Employee";
            cmd.Parameters.Clear();
            cmd.Parameters.AddRange(p);
            i = cmd.ExecuteNonQuery();
            return i;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            cmd.Dispose();
            con.Dispose();
        }
    }
My Connection string is :
<add name="connStr" connectionString="DSN=TestODBC;Uid=myUserName;Pwd=MyPassword;"/>

Please note that -
1. my all code is working fine with SqlConnection insted of OdbcConnection.
2. Stored Procedure "Save_Employee" is working fine with SqlConnection insted of OdbcConnection.
3. but when i convert all code to OdbcConnection, the ExecuteNonQuery is success but no any record inserted in database.

Any one can help me...?
Thanks in advance your any kind of help.
Posted 4-Jan-13 1:13am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You must use the full ODBC call syntax. It also doesn't use named parameters, so you would use a question mark in each param's place
cmd.CommandText = "exec Save_Employee ?, ?, ?";
if you had 3 parameters.
 
For further reading:
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbccommand.commandtype.aspx[^]
  Permalink  
Comments
Mr. Mahesh Patel at 4-Jan-13 23:26pm
   
Thanks a lot, I got Solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

How are you passing the parameters that needs to be updated to the stored procedure. I think you are missing a lot of SqlParameters and you also don't need to call the cmd.Parameters.Clear();
 
If your stored procedure is not expecting any parameter from the outside world then perhaps we need to look into the procedure to figure out the problem.
  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 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
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