Solved

ASP.NET SQL Query example

Posted on 2010-08-23
6
649 Views
Last Modified: 2013-11-27
I aplogize, I did not know exactly what I needed to post.  I saw this article which looks like what I'm trying to do:
http://www.mikesdotnetting.com/Article/64/Bind-Data-From-a-SqlDataSource-to-a-Label
I created a database.mdf with a table "Employees" with 3 columns, FirstName, LastName and EmployeeID

I copied this text into a Default.aspx Page:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" /> <asp:Label ID="Label2" runat="server" />


  <asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString2 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString2.ProviderName %>"
SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)"
OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
 
  <asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString3 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString3.ProviderName %>"
DatasourceMode="DataReader"
SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)"
OnSelecting="SqlDataSource2_Selecting">
</asp:SqlDataSource>

    </div>
    </form>
</body>
</html>


I copied this code into the default.aspx.vb page:

Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object,
      ByVal e As SqlDataSourceSelectingEventArgs)
        e.Command.Parameters("EmployeeID").Value = 2
    End Sub
    Protected Sub SqlDataSource2_Selecting(ByVal sender As Object,
      ByVal e As SqlDataSourceSelectingEventArgs)
        e.Command.Parameters("EmployeeID").Value = 2
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dvSql As DataView =
   DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
        For Each drvSql As DataRowView In dvSql
            Label1.Text = drvSql("FirstName").ToString()
        Next

        Dim rdrSql As OleDbDataReader =
         DirectCast(SqlDataSource2.Select(DataSourceSelectArguments.Empty), OleDbDataReader)
        While rdrSql.Read()

            Label2.Text = rdrSql("LastName").ToString()
        End While
        rdrSql.Close()
    End Sub

End Class


I get this error message:

System.IndexOutOfRangeException was unhandled by user code
  Message=An SqlParameter with ParameterName 'EmployeeID' is not contained by this SqlParameterCollection.
  Source=System.Data
  StackTrace:
       at System.Data.SqlClient.SqlParameterCollection.GetParameter(String parameterName)
       at System.Data.Common.DbParameterCollection.get_Item(String parameterName)
       at _Default.SqlDataSource1_Selecting(Object sender, SqlDataSourceSelectingEventArgs e) in E:\WebSites\Simple SQL table lookup\Default.aspx.vb:line 9
       at System.Web.UI.WebControls.SqlDataSourceView.OnSelecting(SqlDataSourceSelectingEventArgs e)
       at System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
       at System.Web.UI.WebControls.SqlDataSource.Select(DataSourceSelectArguments arguments)
       at _Default.Page_Load(Object sender, EventArgs e) in E:\WebSites\Simple SQL table lookup\Default.aspx.vb:line 17
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

I know I'm doing something really dumb.  Can anyone please help?  Thanks
0
Comment
Question by:Bocefus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 21

Expert Comment

by:chapmanjw
ID: 33507767
You have not specified where the EmployeeID parameter is coming from.  Here is sample code from Microsoft that shows you how to specify the parameters: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selectparameters.aspx
0
 
LVL 7

Expert Comment

by:kovilpattiBalu
ID: 33521532
hi,

In your select querys include "EmployeeID" as parameter.
Ie: SqlCommand cmdInsert = new SqlCommand(sqlInsert, sqlConnection);
cmdInsert.Parameters.Add("@EmployeeID", SqlDbType.NVarChar, 20, "EmployeeID");
0
 

Author Comment

by:Bocefus
ID: 33536893
kovilpattiBalu
Where would I add this?  
 
0
 
LVL 21

Accepted Solution

by:
chapmanjw earned 500 total points
ID: 33537539
In between the  and  tags you would add a
 tag.  For examples, see: http://msdn.microsoft.com/en-us/library/z72eefad.aspx
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question