Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem With Update
Message
 
To
02/12/2009 17:11:51
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
01437326
Message ID:
01437391
Views:
46
I found the problem, but I need input on what to do:

Here is my ExecuteQuery method:
public DataSet ExecuteQuery(string sCommand, string sTableName, ArrayList colParams, CommandType Type)
{
    DataSet oRetVal = new DataSet();
    SqlConnection oConn = _GetConnection(false);

    if (_oException == null)
    {
        if (sTableName == null)
        {
            sTableName = "Table"; 
        }

        SqlCommand oCommand = new SqlCommand();
        oCommand.CommandText = sCommand;
        oCommand.Connection = oConn;

        if (colParams != null)
        {
            SqlParameter[] aParams = new SqlParameter[colParams.Count];
            colParams.CopyTo(aParams);

            oCommand.Parameters.Clear();
            oCommand.Parameters.AddRange(aParams);
        }

        oCommand.CommandType = Type;

        _oAdapter = new SqlDataAdapter();
        _oAdapter.SelectCommand = oCommand;

        SqlCommandBuilder oBuilder = new SqlCommandBuilder(_oAdapter);

        try
        {
            _oAdapter.Fill(oRetVal, sTableName);
        }
        catch (SqlException e)
        {
            _oException = e;
        }
        finally
        {
            if (oConn.State == ConnectionState.Open)
            {
                oConn.Close();
            }
        }

        if (_oException == null)
        {
            oCommand.Parameters.Clear();
        }

    }

    return oRetVal;
}
Notice that at the bottom I clear the parameters collection. I did this because once a SqlParameter instance is assigned to a command object, it cannot be used again.

So, if I had a SqlParameter instance for a parameter called Key, and the key applied to multiple queries, I was simply reusing the
SqlParameter instance each time, rather than create a new one with a difference name:
SqlParameter pKey = new SqlParameter("RowId", iRowId);
ArrayList colParams = new ArrayList();
colParams.Add(pKey);

DataSet dsCust = DataProcs.ExecuteQuery("wit_GetCustomer", colParams);
DataSet dsInv = DataProcs.ExecuteQuery("wit_GetCustInvoices", colParams);
By clearing out the paramaters collection at the end of ExecuteQuery I can reuse the SqlParameters instance. Otherwise, youi
throw an error because the param is already assigned to a command object.

Soooo, since I cleared the paramaters collection when Update is called it fails.

I'm open to suggestion.



>How have you set up the commands that the DataAdapter expects to be set when using its .Update() method? Are you using a CommandBuilder? Have you set it up yourself? Have you read my blog about DataAccess? (3-part series: http://geek-goddess-bonnie.blogspot.com/2009/09/dataaccess-part-i.html)
>
>~~Bonnie
>
>
>>I am calling a stored proc to get back a DS of data:
>>
>>
>>CREATE PROCEDURE wit_GetPickListData
>>	@ItemType	VARCHAR(20)
>>
>>AS
>>BEGIN
>>
>>	SELECT ItemKey, ItemType, Caption, ItemDesc
>>		FROM wit_PickLists
>>		WHERE ItemType = @ItemType
>>		ORDER BY Caption
>>
>>END
>>
>>
>>I then bind the data set to a listbox and a text box:
>>
>>
>>// Bind the data set to the listbox
>>lstItems.DataSource = dsItems.Tables[0];
>>lstItems.DisplayMember = "Caption";
>>
>>lstItems.SelectedIndex = -1;
>>
>>// Bind the caption
>>txtCaption.DataBindings.Add("Text", dsItems.Tables[0], "Caption");
>>
>>
>>After I change the value in the textbox I can see te change in the DS using the visualizer. I call UpdateDataSet:
>>
>>
>>public void UpdateDataSet(DataSet oDS, string sTableName)
>>{
>>    if (sTableName == null)
>>    {
>>        sTableName = oDS.Tables[0].TableName;
>>    }
>>
>>    DataTable oTable = oDS.Tables[sTableName];
>>
>>    foreach (DataRow oRow in oTable.Rows)
>>    {
>>        oRow.EndEdit();
>>    }
>>
>>    try
>>    {
>>        _oAdapter.Update(oDS, sTableName);
>>    }
>>    catch (SqlException e)
>>    {
>>        throw e;
>>    }
>>}
>>
>>
>>
>>It's erroring with "Procedure or function 'wit_GetPickListData' expects parameter '@ItemType', which was not supplied."
>>
>>I don't understand this at all. Why does .Net care about this proc at this point?? What am I doing wrong here???
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform