Hi,
I am using this below codes to add new items to the grid row and its not getting inserted. I don't know what mistake I done in the code. Can anyone correct me?
code behind(UI):
protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox EmpID = (TextBox)GridView1.FooterRow.FindControl("txtEmpID");
TextBox Name = (TextBox)GridView1.FooterRow.FindControl("txtName");
TextBox Designation = (TextBox)GridView1.FooterRow.FindControl("txtDesignation");
TextBox City = (TextBox)GridView1.FooterRow.FindControl("txtCity");
AppGridData.InsertGridValues(EmpID.Text, Name.Text, Designation.Text, City.Text);
FillVendorGrid();
}
}
BLL:
public static bool InsertGridValues(string EmpID, string Name, string Description, string City)
{
return AppGridRepository.InsertGridValues(EmpID, Name, Description, City);
}
DAL:
public static bool InsertGridValues(string EmpID, string Name, string Description, string City)
{
string CS = ConfigurationManager.ConnectionStrings[Constants.ConnectionString].ConnectionString;
using (SqlConnection connection = new SqlConnection(CS))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "INSERT INTO [AppInvent_Test]([EmpID],[Name],[Description],[City]) Values('" + EmpID + "','" + Name + "','" + Description + "', '" + City + "')";
connection.Open();
command.ExecuteNonQuery();
}
return true;
}
Loading
Sanjeeb LenkaPosted Oct 28, 2013, 8:06 AM
may be some comma i missed. check this
command.CommandText = "UPDATE [AppInvent_Test] SET [Name]='" + Name + "',[Designation]= '" + Designation + "' , [City]= '" + City + "' WHERE EmpID='"+Empid+"'";
Vignesh KumarPosted Oct 28, 2013, 8:46 AM
Vignesh KumarPosted Oct 28, 2013, 7:44 AM
I get an error, "Incorrect syntax near Designation".
Sanjeeb LenkaPosted Oct 28, 2013, 6:53 AM
you have to pass the empid to update query to update the data.
check your update query it looks wrong. try like this
"UPDATE [AppInvent_Test] SET [Name]='" + Name + "'[Designation]= '" + Designation + "' and [City]= '" + City + "' WHERE EmpID='"+Empid+"'";
Vignesh KumarPosted Oct 28, 2013, 6:50 AM
public static bool UpdateGridValues(string Name, string Designation, string City)
{
string strcon = ConfigurationManager.ConnectionStrings[Constants.Corp_CMDBConnectionString].ConnectionString;
using (SqlConnection connection = new SqlConnection(strcon))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "UPDATE [AppInvent_Test] SET [Name]='" + Name + "' WHERE ([Designation]= '" + Designation + "' and [City]= '" + City + "')";
connection.Open();
command.ExecuteNonQuery();
}
return true;
}
Vignesh KumarPosted Oct 28, 2013, 6:37 AM
All worked except update function. Update function is not working in the grid.
Rest all are good.
I do have four column
EmpID, Name, Designation, City
I am updating these columns, also EmpID is an identity column and should I have to pass that parameter here in the code?
public static bool UpdateGridValues(string Name, string Designation, string City)
{
string strcon = ConfigurationManager.ConnectionStrings[Constants.Corp_CMDBConnectionString].ConnectionString;
using (SqlConnection connection = new SqlConnection(strcon))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "UPDATE [AppInvent_Test] SET [Name]='" + Name + "' WHERE ([Designation]= '" + Designation + "' and [City]= '" + City + "')";
connection.Open();
command.ExecuteNonQuery();
}
return true;
}
Sanjeeb LenkaPosted Oct 28, 2013, 6:10 AM
in .aspx page you are missing the body tag and form tag:
check this:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AppGrid.aspx.cs" Inherits="METLIFE.CSPM.AppInventory.UI.AppGrid" %>
cellpadding="4" onrowdeleting="ResultGridView_RowDeleting" onrowediting="ResultGridView_RowEditing"
onrowupdating="ResultGridView_RowUpdating" onrowcancelingedit="ResultGridView_RowCancelingEdit"
onrowcommand="ResultGridView_RowCommand" pagesize="100" forecolor="#333333" gridlines="None">
Sanjeeb LenkaPosted Oct 28, 2013, 3:43 AM
Vignesh KumarPosted Oct 28, 2013, 3:17 AM
I kept break point and checked and its not coming to the breakpoint. The grid loads and binds the data from DB. But when I click on add new, delete and edit button in the grid in the web browser nothing happens. Also when I click on AddNew button, I get an error "Error on page" at bottom left of the page
Sanjeeb LenkaPosted Oct 28, 2013, 3:12 AM
put a break point and check its inserting data into database or not.