We help IT Professionals succeed at work.

Unable to show details in DetailView- how to supply parameter

230 Views
Last Modified: 2013-12-17
I am coding in C#, visual studio 2005.   I sucessfully created the data for my datagrid.  I created the connection string, an adapter, dataset and filled the dataset before binding to the mainGrid.

I created a detailsView in the   (I still have more to do with this application, like update, and remove) and repeated the same actions.  only with my adapter, i have a parameter.  However, when i run the app, I get:
"Expects Parameter which was not supplied"

How would I supply the parameter?

===========This is my code-behind: =====================

public partial class ReqsDubs : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            bindGrid();
        }
}
        private void bindGrid()
        {        
   string tapelibraryconn = ConfigurationManager.ConnectionStrings["tapelibraryconn"].ConnectionString;

//create dataset
   DataSet ds = new DataSet();

//create the connection string
SqlConnection conn = new SqlConnection(tapelibraryconn);

//create adapter
SqlDataAdapter myadap = new SqlDataAdapter("select ID as [Request ID], Name, [Request date] from view_DubRequests WHERE Status = 1", conn);

 //fill the dataset
myadap.Fill(ds, "view_DubRequests");
    mainGrid.DataSource = ds;
    mainGrid.DataBind();
    }

//this works fine
    protected void mainGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        //obtain the index of the selected table
        int selectedRowIndex;

        selectedRowIndex = mainGrid.SelectedIndex;

        //get information about record selected
        GridViewRow row = mainGrid.Rows[selectedRowIndex];

    //Old Code that worked to show the data before defining a new connection
       /*string name = row.Cells[3].Text;
        string showID = row.Cells[4].Text;
        errorLbl.Text = "Tomeka, you selected " + showID + "'s " + name + " record for editing" ;
       */

        //read the ID
        int RequestID = System.Convert.ToInt16(mainGrid.SelectedRow.Cells[3].Text.ToString());

        //define the data objects
        string mytapelibraryconn = ConfigurationManager.ConnectionStrings["tapelibraryconn"].ConnectionString;

        SqlConnection myConn = new SqlConnection(mytapelibraryconn);

        //command object
        SqlCommand myComm = new SqlCommand("Select RequestID, User, Email, Show, Number, Barcode FROM view_DubReqsDetails where RequestID=@RequestID", myConn);

       myComm.Parameters.Add("@RequestID", SqlDbType.Int, 4);
       
        //create adapter
        SqlDataAdapter adap = new SqlDataAdapter(myComm);

        //create new dataset to connect the controls
        DataSet da = new DataSet();

        adap.Fill(da);
        ReqDetails.DataSource = da;
        ReqDetails.DataBind();

       
        //fill the adapter and bind to detailsGrid
        adap.SelectCommand = myComm;

    }
}

Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I tried that but i still get an error on filling the dataset:
The error reads
Failed to convert parameter value from a string to int32

and points to: adap.Fill(da);
Why am I getting this error if RequestID is the ID of the recordset?

Author

Commented:
and after making a few changes, I also get this error:

An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.dll

Author

Commented:
I was able to get the DetailsView to show.  I changed a few other things in my code.  Thanks.
Out of curiosity, what was causing the "Failed to convert parameter value from a string to int32" error?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.