简体   繁体   中英

Exception stored procedure parameter

I have this code

SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter();
cmd.CommandType = CommandType.StoredProcedure;
cmd = new SqlCommand("s_SCT_Composition", conn);

SqlParameter param1 = new SqlParameter("@idServ", SqlDbType.Int);
param1.Value = 1002262;

cmd.Parameters.Add(param1);

adapter.SelectCommand = cmd;

try
{
    adapter.Fill(tableCompositionNomenclature);
}

catch (Exception ex)
{
    MessageBox.Show(ex.ToString());

}
DataGridNameService.ItemsSource = tableCompositionNomenclature.DefaultView;

following exception occurs

procedure or function expects parameter which was not specified

stored procedure:

ALTER PROCEDURE [dbo].[s_SCT_Composition]
 @IdServ Integer    
AS 
``SELECT CAST (0 as bit) as 'chk', dbo.s_Serv.NameServ as 'NameServNomenc', dbo.rs_Serv.IdServ as 'IdServ', dbo.s_serv.IdServ as 'IdServNomenc'
FROM     dbo.rs_Serv INNER JOIN
                  dbo.rs_ServK ON dbo.rs_Serv.IdServ = dbo.rs_ServK.IdServM INNER JOIN
                  dbo.s_Serv ON dbo.rs_ServK.IdServN = dbo.s_Serv.IdServ
                  where dbo.rs_Serv.IdServ = @IdServ;   

whats a problem?

This method create table in my code:

DataColumn column;

column = new DataColumn();
column.DataType = System.Type.GetType("System.Boolean");
column.ColumnName = "chk";
column.ReadOnly = false;
tableCompositionNomenclature.Columns.Add(column);

column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "IdServNomenc";
column.ReadOnly = false;
tableCompositionNomenclature.Columns.Add(column);

column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "NameServNomenc";
column.ReadOnly = true;
tableCompositionNomenclature.Columns.Add(column);

column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "IdServ";
column.ReadOnly = true;

tableCompositionNomenclature.Columns.Add(column);

//DataGridNameService.ItemsSource = tableCompositionNomenclature.DefaultView;
DataGridNameService.CanUserAddRows = false;

this my code create table, It can be a problem in it

I debugged all my code option always is present, but the exception still occurs

tried to change the name of the parameter, the result is the same

tried to change the type of the passed parameter, the result is the same

tried to set two parameters, the result is the same

may be an error in the method Fill, it can not receive parameters?

SqlParameter param1 = new SqlParameter("@idServ", SqlDbType.Int);

You shouldn't prefix your parameter with the @ symbol in this statement.

Also, make sure case-sensitivity isn't the issue. "idServ" != "IdServ"

cmd.CommandType = CommandType.StoredProcedure;
cmd = new SqlCommand("s_SCT_Composition", conn);

This is losing the first line's effect because you have a full new object created.

Try this instead:

cmd = new SqlCommand("s_SCT_Composition", conn);
cmd.CommandType = CommandType.StoredProcedure;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM