3
Answers

Update Data Grid View ASP.NET

Azm Amn

Azm Amn

9y
351
1
Hi i need help in my code. Once i run the program it says 

Must declare the scalar variable "@Police_ID".

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.Data.SqlClient.SqlException: Must declare the scalar variable "@Police_ID".
 
In my DB Police_ID is FK and Victim_ID is PK 
 
Design Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Victim_ID,Police_ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Victim_ID" HeaderText="Victim_ID" ReadOnly="True" SortExpression="Victim_ID" />
<asp:BoundField DataField="Police_ID" HeaderText="Police_ID" ReadOnly="true" SortExpression="Police_ID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="MiddleName" HeaderText="MiddleName" SortExpression="MiddleName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="IC_No" HeaderText="IC_No" SortExpression="IC_No" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="ContactNumber" HeaderText="ContactNumber" SortExpression="ContactNumber" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:PoliceConnectionString %>" DeleteCommand="DELETE FROM [Victim] WHERE [Victim_ID] = @original_Victim_ID AND [Police_ID] = @original_Police_ID AND [FirstName] = @original_FirstName AND [MiddleName] = @original_MiddleName AND [LastName] = @original_LastName AND [IC_No] = @original_IC_No AND [Address] = @original_Address AND [ContactNumber] = @original_ContactNumber" InsertCommand="INSERT INTO [Victim] ([Victim_ID], [Police_ID], [FirstName], [MiddleName], [LastName], [IC_No], [Address], [ContactNumber]) VALUES (@Victim_ID, @Police_ID, @FirstName, @MiddleName, @LastName, @IC_No, @Address, @ContactNumber)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Victim]" UpdateCommand="UPDATE [Victim] SET [Police_ID] = @Police_ID, [FirstName] = @FirstName, [MiddleName] = @MiddleName, [LastName] = @LastName, [IC_No] = @IC_No, [Address] = @Address, [ContactNumber] = @ContactNumber WHERE [Victim_ID] = @original_Victim_ID AND [Police_ID] = @original_Police_ID AND [FirstName] = @original_FirstName AND [MiddleName] = @original_MiddleName AND [LastName] = @original_LastName AND [IC_No] = @original_IC_No AND [Address] = @original_Address AND [ContactNumber] = @original_ContactNumber">
<DeleteParameters>
<asp:Parameter Name="original_Victim_ID" Type="String" />
<asp:Parameter Name="original_Police_ID" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_MiddleName" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
<asp:Parameter Name="original_IC_No" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
<asp:Parameter Name="original_ContactNumber" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Victim_ID" Type="String" />
<asp:Parameter Name="Police_ID" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="MiddleName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="IC_No" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="ContactNumber" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Police_ID" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="MiddleName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="IC_No" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="ContactNumber" Type="String" />
<asp:Parameter Name="original_Victim_ID" Type="String" />
<asp:Parameter Name="original_Police_ID" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_MiddleName" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
<asp:Parameter Name="original_IC_No" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
<asp:Parameter Name="original_ContactNumber" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
 
 
Behind Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace Final_Project{
public partial class Update_Victim_Details : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=AZM\\AZM;Initial Catalog=Police;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) // If you not place this check then you will get the old values because GridView in Bind on every postback
{
DataBind();
}
}
private void BindGrid() // function for binding gridview
{
DataTable dt = new DataTable();
dt.Columns.Add("Victim_ID");
dt.Columns.Add("Police_ID");
dt.Columns.Add("FirstName");
dt.Columns.Add("MiddleName");
dt.Columns.Add("LastName");
dt.Columns.Add("IC_No");
dt.Columns.Add("Address");
dt.Columns.Add("ContactNumber");
DataRow r = dt.NewRow();
r[0] = "Victim_ID 1";
r[1] = "Police_ID 1";
r[2] = "FirstName 1";
r[3] = "MiddleName 1";
r[4] = "LastName 1";
r[5] = "IC_No 1";
r[6] = "Address 1";
r[7] = "ContactNumber 1";
DataRow r1 = dt.NewRow();
r1[0] = "Victim_ID 2";
r1[1] = "Police_ID 2";
r1[2] = "FirstName 2";
r1[3] = "MiddleName 2";
r1[4] = "LastName 2";
r1[5] = "IC_No 2";
r1[6] = "Address 2";
r1[7] = "ContactNumber 2";
dt.Rows.Add(r);
dt.Rows.Add(r1);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; // setting new index
BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string newvalue = ((TextBox)row.Cells[0].Controls[0]).Text;
GridView1.EditIndex = -1; // Again reset
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; // reseting grid view
BindGrid();
}
}
}
 
 
 Please Help me i am doing my final project need help please
 
 

Answers (3)