My Update function not working. please help me to solve this. no any error message show.
CREATE TABLE [dbo].[dly_prm_dta2] (
[Flw_mtr] VARCHAR (100) NOT NULL,
[param_dt] DATE NOT NULL,
[param_dept] VARCHAR (50) NOT NULL,
[rd_one] NUMERIC (12, 2) NOT NULL,
[rd_two] NUMERIC (12) NOT NULL,
[diff] NUMERIC (12) NOT NULL,
[rmk] VARCHAR (500) NOT NULL,
[entry_by] VARCHAR (50) NOT NULL,
[entry_dt] DATE NOT NULL,
[is_updated] VARCHAR (3) DEFAULT ('No') NULL,
CONSTRAINT [PK_dly_prm_dta1] PRIMARY KEY CLUSTERED ([Flw_mtr] ASC, [param_dt] ASC)
);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace ceo_dshbrd
{
public partial class home : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(@"Data Source=10.1.246.4;Initial Catalog=ceodshbod;User ID=dba_ntpc_meja_intranet;Password=alpha$890;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
protected void Page_Load(object sender, EventArgs e)
{
Label3.Text = System.DateTime.Now.ToString("dd-MM-yyyy");
string encodedDate = Request.QueryString["date"];
string selectedDate = HttpUtility.UrlDecode(encodedDate);
lblSelectedDate.Text = selectedDate;
if (!this.IsPostBack)
{
this.BindData();
if (Session["emp_id"] == null)
Response.Redirect("snsout.aspx");
else
{
String EmplooyeId = Session["emp_id"].ToString();
Label1.Text = " " + EmplooyeId + "";
String param_dept = Session["param_dept"].ToString();
Label2.Text = "" + param_dept + " ";
// String dept = Session["dept"].ToString();
//Label3.Text = "" + dept + " ";
}
}
}
protected void BindData()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
DateTime dt1 = DateTime.ParseExact(lblSelectedDate.Text, "dd-MM-yyyy", null);
String dt2 = dt1.ToShortDateString();
//using (SqlCommand cmd = new SqlCommand("SELECT Flw_mtr, param_dept,rmk,rd_one,rd_two,diff FROM dly_prm_dta1 where param_dept='" + Session["param_dept"] + "'And param_dt= @pdatelblSelectedDate.Text + "'", con))
String sqlCommandText = "SELECT Flw_mtr, param_dept,rmk,rd_one,rd_two,diff FROM dly_prm_dta2 where param_dept='" + Session["param_dept"] +
"'And param_dt= '" + dt2 + "'";
using (SqlCommand cmd = new SqlCommand(sqlCommandText, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
string pd = Session["param_dept"].ToString();
//DateTime dt1 = DateTime.ParseExact(lblSelectedDate.Text, "MM/dd/yyyy", null);
//cmd.Parameters.AddWithValue("@param_dept", Session["param_dept"]);
//cmd.Parameters.AddWithValue("@param_dt", DateTime.ParseExact(lblSelectedDate.Text,"dd-MM-yyyy",null));
sda.SelectCommand = cmd;
con.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
sda.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("caldr.aspx");
}
protected void Button2_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
conn.Open();
foreach (GridViewRow row in GridView1.Rows)
{
// GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
string Flw_mtr = (row.FindControl("txtparam_dsc") as Label).Text;
string param_dept = (row.FindControl("txtdeptt") as Label).Text;
string rd_one = (row.FindControl("txtrdone") as TextBox).Text;
string rd_two = (row.FindControl("txtredtw") as TextBox).Text;
string diff = (row.FindControl("txtdiff") as TextBox).Text;
string rmk = (row.FindControl("txtparam_rmrk") as TextBox).Text;
string entry_by = Label1.Text;
DateTime entry_dt = DateTime.Today;
// DateTime entry_dt =Label3.Text.ToString());
DateTime param_dt = DateTime.ParseExact(lblSelectedDate.Text, "dd-MM-yyyy", null);
Object a;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
/* Check if data exists. If data exits, then update else insert */
using (SqlCommand cmd = new SqlCommand("select count(*) From dly_prm_dta2 where Flw_mtr = @Flw_mtr and param_dt = @param_dt"))
{
cmd.Parameters.AddWithValue("@Flw_mtr", Flw_mtr);
cmd.Parameters.AddWithValue("@param_dt", param_dt);
cmd.Connection = con;
con.Open();
a = cmd.ExecuteScalar();
con.Close();
}
SqlCommand cmd1;
if (a.ToString().Equals("1"))
{
cmd1 = new SqlCommand("update dly_prm_dta2 set rd_two = @rd_two ,diff = @diff,entry_by = @entry_by, entry_dt = @entry_dt where flw_mtr = @Flw_mtr and param_dt = @param_dt");
cmd1.Parameters.AddWithValue("@Flw_mtr", Flw_mtr);
cmd1.Parameters.AddWithValue("@rd_one", rd_one);
cmd1.Parameters.AddWithValue("@rd_two", rd_two);
cmd1.Parameters.AddWithValue("@diff", diff);
cmd1.Parameters.AddWithValue("@rmk", rmk);
cmd1.Parameters.AddWithValue("@entry_by", entry_by);
cmd1.Parameters.AddWithValue("@entry_dt", entry_dt);
cmd1.Parameters.AddWithValue("@param_dt", param_dt);
cmd1.Connection = con;
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
else
{
cmd1 = new SqlCommand("update dly_prm_dta2 set rd_one = @rd_two ,diff = rd_two - @rd_two, param_dt=@param_dt,entry_by = @entry_by, entry_dt = @entry_dt where flw_mtr = @Flw_mtr");
cmd1.Parameters.AddWithValue("@Flw_mtr", Flw_mtr);
cmd1.Parameters.AddWithValue("@rd_two", rd_two);
cmd1.Parameters.AddWithValue("@entry_by", entry_by);
cmd1.Parameters.AddWithValue("@entry_dt", entry_dt);
param_dt = param_dt.AddDays(1);
cmd1.Parameters.AddWithValue("@param_dt", param_dt);
cmd1.Connection = con;
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
}
}
}
}
}