CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2001
    Location
    jordan
    Posts
    63

    Insert Row using C# and a stored procedure

    I have this stored procedure

    CREATE PROCEDURE [dbo].InsertUserTables
    @UserName varchar(100),
    @View varchar(30)
    AS


    INSERT INTO [dbo].[UserTables] (
    [UserName],
    [View]
    )
    VALUES (
    @UserName,
    @View
    )
    GO



    and the C# code to insert a row

    Code:
        System.Data.SqlClient.SqlParameter [] arParams2 = new      System.Data.SqlClient.SqlParameter[2];
    
        arParams[0] = new System.Data.SqlClient.SqlParameter("@UserName", System.Data.SqlDbType.NVarChar, 100);
    
        arParams[0].Value = "awni";
        arParams[0].Direction = System.Data.ParameterDirection.Input;
    
          arParams[1] = new System.Data.SqlClient.SqlParameter("@View", System.Data.SqlDbType.NVarChar, 30);
    
          arParams[1].Value = "Cars";
          arParams[1].Direction = System.Data.ParameterDirection.Input;
    
          Insert("InsertUserTables", arParams);
    
    
    ..........
    
    
    public void Insert(string szSPName, SqlParameter [] arParms)
    {		
    
    
    // SqlConnection that will be used to execute the sql commands SqlConnection connection = null; connection = GetConnection(CGlobals.szConnectionString); SqlCommand sqlCmd = new SqlCommand(szSPName, connection); sqlCmd.CommandType = CommandType.StoredProcedure; //sqlCmd.Connection.Open(); foreach (SqlParameter p in arParms) {
    if( p != null ) { // Check for derived output value with no value assigned if ( ( p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input ) && (p.Value == null)) {
    p.Value = DBNull.Value;
    } sqlCmd.Parameters.Add(p); }
    } sqlCmd.ExecuteNonQuery(); connection.Close();
    } I've omitted the try/catch blocks here, but in the my code, I use them and I get "Object must implement IConvertible" exception, what can I do? What object is it talking about? thanks awni

  2. #2
    Join Date
    Jul 2001
    Location
    jordan
    Posts
    63

    Re: Insert Row using C# and a stored procedure

    sorry about that, i just noticed that i'm using arParams instead of arParams2.
    both arrays are declared, but arParams is for somehthing else, so that's why it's not working. (I hope i didn't make you scratch your head too much).

    thank you for your time

    awni

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured