none
cannot to insert @parametr to sql table in c# code. RRS feed

  • Question

  • Hi to all.

    I have this code all works fine but in the end I receive error that "Procedure or Function 'UpdateCorporationFile' expects parametr '@request_t' wich not was supplied.

    But If I check the perametrs in the cmd point I see the value.

    private void StoreData()

            {

                SqlConnection conn = null;

     

                try

                {

                                    conn = new SqlConnection("Data Source=PC;Initial Catalog=DMS;Integrated Security=True");

                    conn.Open();

                    SqlCommand cmd = new SqlCommand("dbo.UpdateCorporationFile", conn);

                    cmd.CommandType = CommandType.StoredProcedure;

     

                    cmd.Parameters.Add(new SqlParameter("@request_t", ClientData.RequestType.ToString()));

     

     

     

     

                    SqlDataAdapter sqlDa2 = new SqlDataAdapter();

                    sqlDa2.InsertCommand = cmd; I “CMD” I can see that Value present inside.

     

                    int num = sqlDa2.InsertCommand.ExecuteNonQuery();

                    string rowNumAffected = Convert.ToString(num);

                          }

     

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

     

                finally

                {

                    if (conn != null)

                    {

                        conn.Close();

                    }

                }

            }

     

     

     

     

     

     


    Dan
    • Moved by Sheldon _Xiao Monday, June 20, 2011 5:07 AM (From:Windows Presentation Foundation (WPF))
    Wednesday, June 15, 2011 4:50 PM

Answers

All replies

  • Hey Dan,

    This should probably be moved to the ADO.NET forums instead of being in the WPF forums. Maybe if an admin sees this they can move it so you can get some better suited experts to take a look.

    In your example im not sure why you are creating a SqlDataAdapter, i think you could just call cmd.ExecuteNonQuery() to get the same result without the need to create the adapter. Also make sure your ClientData.RequestType value is not null.

    -Stephen

    Wednesday, June 15, 2011 7:36 PM
  • you have @parameters inside your query, thus you need to provide values for those parameters via Parameters array of DataAdapter. Here you can read more information about it.
    Tamir http://khason.net/
    If your question was answered, please mark it.
    Wednesday, June 15, 2011 8:21 PM
  • Hi to all.

    I have this code all works fine but in the end I receive error that "Procedure or Function 'UpdateCorporationFile' expects parametr '@request_t' wich not was supplied.

    But If I check the perametrs in the cmd point I see the value.

    private void StoreData()

            {

                SqlConnection conn = null;

     

                try

                {

                                    conn = new SqlConnection("Data Source=PC;Initial Catalog=DMS;Integrated Security=True");

                    conn.Open();

                    SqlCommand cmd = new SqlCommand("dbo.UpdateCorporationFile", conn);

                    cmd.CommandType = CommandType.StoredProcedure;

     

                    cmd.Parameters.Add(new SqlParameter("@request_t", ClientData.RequestType.ToString()));

     

     

     

     

                    SqlDataAdapter sqlDa2 = new SqlDataAdapter();

                    sqlDa2.InsertCommand = cmd; In “CMD properties” I can see that Value present inside but dispite I receive error message that SP expect the parametr.

     

                    int num = sqlDa2.InsertCommand.ExecuteNonQuery();

                    string rowNumAffected = Convert.ToString(num);

                          }

     

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

     

                finally

                {

                    if (conn != null)

                    {

                        conn.Close();

                    }

                }

            }

     


    Dan
    Thursday, June 16, 2011 10:44 AM
  • I wonder if using a SqlDataAdapter when you don't need to is causing some issues? As @sehlers suggested, you don't need that adapter when all you simply need to do is to call the cmd.ExecuteNonQuery(). I wouldn't think that it would matter, but try it without bothering with the SqlDataAdapter and the InsertCommand. Just use your original cmd.

    The other thing that @sehlers mentioned was about the parameter value ClientData.RequestType, being sure it is not == null (although, I think you'd get a different error if that were the case). I'd also recommend using the cmd.Parameters.AddWithValue() method instead of the .Add() method. I think it's much cleaner:

    cmd.Parameters.AddWithValue("@request_t", ClientData.RequestType.ToString());


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, June 29, 2011 5:37 PM