1

The code is as follows... I tried using debug points and it shows:

Procedure or function 'usp_select_legal1_data' expects parameter '@tower', which was not supplied.

C# code:

try {
    LegalView.ActiveViewIndex = 2;

    String tw2 = TextBox3.Text;
    SqlDataSource SqlDataSource2 = new SqlDataSource();
    SqlDataSource2.ID = "SqlDataSource2";
    this.Page.Controls.Add(SqlDataSource2);
    SqlDataSource2.ConnectionString = System.Configuration.ConfigurationManager
        .ConnectionStrings["constr"].ConnectionString;
    SqlDataSource2.SelectParameters.Add("@tower", tw2);
    SqlDataSource2.SelectCommand = "usp_select_legal1_data";

    GVCaseTowerWise.DataSource = SqlDataSource2;
    GVCaseTowerWise.DataBind();

    if (GVCaseTowerWise.Rows.Count == 0) {
        ScriptManager.RegisterClientScriptBlock(
            this,
            this.GetType(),
            "alertMessage",
            "alert('No cases for this tower exist in the database')",
            true);
    }
} catch (Exception ex) {
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
            "alertMessage", "alert('error while getting data')", true);
}

And this is my stored procedure:

ALTER PROCEDURE [dbo].[usp_select_legal1_data]
    @tower nvarchar(50)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT distinct
      [tower_to]
      ,[tower_from]
      ,[sy_no]
    FROM [dbo].[legal1]
    WHERE ((tower_to = @tower) or (tower_from = @tower))
END
1
  • 1
    tw2 is probably null and @tower has no default. Commented Jun 16, 2015 at 9:53

3 Answers 3

3

Change

SqlDataSource2.SelectParameters.Add("@tower", tw2);

to

SqlDataSource2.SelectParameters.Add("tower", tw2);
Sign up to request clarification or add additional context in comments.

2 Comments

Can you put a breakpoint after SqlDataSource2.SelectParameters.Add("tower", tw2); and see what SelectParameters contains?
guys...can using the 'using' code to open the connection solve the problem?...if not then can there be any software problem that it isn't returning the value...but using direct commands like "select * from...." gets the correct result.
1

The problem is putting @, because the parameter name is tower. please see the MSDN documentation below https://msdn.microsoft.com/en-us/library/f58z9c1a(v=vs.110).aspx

1 Comment

guys...can using the 'using' code to open the connection solve the problem?...if not then can there be any software problem that it isn't returning the value...but using direct commands like "select * from...." gets the correct result.
0

I found the answer to my question. The problem was with defining the stored procedure. it was still treating the procedure as text command. here is what i used

SqlDataSource2.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;

and now it is working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.