Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.

C#
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 :
C#
<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

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[^]
 
Share this answer
 
Comments
Mr. Mahesh Patel 4-Jan-13 23:26pm    
Thanks a lot, I got Solution.
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900