Hi All
i have this stored procedure :
USE [TestDB]
GO
/****** Object: StoredProcedure [dbo].[SP_GetAssetList] Script Date: 05/12/2016 20:56:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SP_GetAssetList]
As
Begin
Select
Asset_ID,Asset_Name, Asset_SN, Asset_Make,Asset_Model, Emp_ID_FK, Asset_Status , Make, Model,Emp_Name
from tbl_Asset
left join tbl_Make on Asset_Make=Make_ID
left join tbl_Model on Asset_Model=Model_ID
full outer join tbl_Employee on Emp_ID_FK=Emp_ID
where asset_id != 0
end
-- i have two tables tbl_Make to which has make_id and Make column
-- and table tbl_model which has model_id and modle as column
and i have this page :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdateAssets.aspx.cs" Inherits="Admin_UpdateAssets" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlEditAsset" Visible="false" runat="server">
<table cellspacing="1" class="auto-style1">
<tr>
<td class="auto-style2">Asset Name:</td>
<td>
<asp:TextBox ID="txtAssetName" runat="server" Width="248px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Asset SN:</td>
<td>
<asp:TextBox ID="txtAssetSN" runat="server" Width="248px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Make:</td>
<td>
<asp:DropDownList ID="ddlMake" runat="server" DataSourceID="dsDDLMake" DataTextField="Make" DataValueField="Make_ID" Height="18px" Width="164px">
</asp:DropDownList>
<asp:SqlDataSource ID="dsDDLMake" runat="server" ConnectionString="<%$ ConnectionStrings:Test_Connection %>" SelectCommand="SP_ddlMake" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="auto-style2">Model:</td>
<td>
<asp:DropDownList ID="ddlModel" runat="server" DataSourceID="dsDDLModel" DataTextField="Model" DataValueField="Model_ID" Height="16px" Width="164px">
</asp:DropDownList>
<asp:SqlDataSource ID="dsDDLModel" runat="server" ConnectionString="<%$ ConnectionStrings:Test_Connection %>" SelectCommand="SP_ddlModel" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="auto-style2">Assigned To:</td>
<td>
<asp:DropDownList ID="ddlEmpList" runat="server" DataSourceID="dsDDLempList" DataTextField="Emp_Name" DataValueField="Emp_ID" Height ="16px" Width="164px">
</asp:DropDownList>
<asp:SqlDataSource ID="dsDDLempList" runat="server" ConnectionString="<%$ ConnectionStrings:Test_Connection %>" SelectCommand="SP_ddlEmpList" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
</td>
</tr>
</table>
</asp:Panel>
<br />
<br />
<asp:GridView ID="GVAssetList" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
<Columns>
<asp:BoundField DataField="Asset_ID" HeaderText="Asset ID" />
<asp:BoundField DataField="Asset_name" HeaderText="Asset Name" />
<asp:BoundField DataField="Asset_SN" HeaderText="Asset SN" />
<asp:BoundField DataField="Make" HeaderText="Asset Make" />
<asp:BoundField DataField="Model" HeaderText="Asset Model" />
<asp:BoundField DataField="Emp_Name" HeaderText="Assigned to" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect" Text="Select" runat="server" OnClick="btnSelect_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
<table>
<tr>
<td class="auto-style2" colspan="2">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
and the code behind the page is as below :
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin_UpdateAssets : System.Web.UI.Page
{
#region Connection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test_Connection"].ConnectionString);
#endregion Connection
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable("GVAssetList-dt");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "SP_GetAssetList";
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GVAssetList.DataSource = dt;
GVAssetList.DataBind();
con.Close();
}
}
protected void btnSelect_Click(object sender, EventArgs e)
{
GridViewRow SelectedRow = ((sender as Button).NamingContainer as GridViewRow);
pnlEditAsset.Visible = true;
txtAssetName.Text = SelectedRow.Cells[1].Text;
txtAssetSN.Text = SelectedRow.Cells[2].Text;
ddlMake.SelectedValue = SelectedRow.Cells[3].ToString();
//ddlModel.SelectedValue = SelectedRow.Cells[4].ToString();
//ddlEmpList.SelectedValue = SelectedRow.Cells[5].ToString();
}
//protected void GVAssetList_SelectedIndexChanged(object sender, EventArgs e)
//{
// Session["GvAssetList_ID"] = GVAssetList.SelectedDataKey.Value;
// try
// {
// SqlCommand cmd = con.CreateCommand();
// cmd.CommandText = "SP_GetAssetDetails";
// cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.AddWithValue("@Asset_ID", Convert.ToInt32(Session["GvAssetList_ID"]));
// con.Open();
// SqlDataReader My = cmd.ExecuteReader();
// if (My.Read())
// {
// txtAssetName.Text = My["Asset_Name"].ToString();
// txtAssetSN.Text = My["Asset_SN"].ToString();
// ddlMake.SelectedValue = My["Asset_Make"].ToString();
// ddlModel.SelectedValue = My["Asset_Model"].ToString();
// ddlEmpList.SelectedValue = My["Emp_ID_FK"].ToString();
// }
// con.Close();
// }
// catch
// {
// }
//}
protected void btnUpdate_Click(object sender, EventArgs e)
{
Session["GVAssetList_ID"] = GVAssetList.SelectedDataKey.Value;
try
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SP_EditAsset";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Asset_ID", Convert.ToInt32(Session["GVAssetList_ID"]));
cmd.Parameters.AddWithValue("@Asset_Name", txtAssetName.Text);
cmd.Parameters.AddWithValue("@Asset_SN", txtAssetSN.Text);
cmd.Parameters.AddWithValue("@Asset_Make", ddlMake.SelectedValue);
cmd.Parameters.AddWithValue("@Asset_Model", ddlModel.SelectedValue);
// cmd.Parameters.AddWithValue("@Emp_ID_FK", ddlEmpList.SelectedValue);
//cmd.Parameters.AddWithValue("@Asset_Status", ddlAssetStatus.SelectedValue);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
con.Close();
lblmsg.Text = "Asset Update!!";
GVAssetList.DataBind();
}
catch
{
}
}
}
when i run the page i get this error :
Server Error in '/' Application.
'ddlMake' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'ddlMake' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentOutOfRangeException: 'ddlMake' has a SelectedValue which is invalid because it does not exist in the list of ite
why this happens and how it solve?
many thanks