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:
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
Upendra Pratap ShahiPosted Jul 29, 2015, 1:43 AM
Rajeesh MenothPosted Jul 29, 2015, 12:26 AM
Raja TPosted Jul 29, 2015, 12:26 AM
Police_ID as int
Police_ID
as string while in grid update it is int, it should be same datatype...make it int.Police_ID
the above mentioned error will occour