Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET SQL Query example

Posted on 2010-08-23
6
Medium Priority
?
661 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 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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