I make that but I did not work, thanks
SqlCommand cmd2;cmd = new SqlCommand("SELECT CS_ID FROM Tbl_Conselheiros WHERE CS_TIPO IN (5,6,7)", conn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
cmd2 = new SqlCommand("SP_PRES_GRAVAR", conn);cmd2.CommandType =
CommandType.StoredProcedure;SqlParameter id = new SqlParameter("@ID_CONSE", SqlDbType.Int, 4);id.Value = Convert.ToInt32(dr[0]);cmd2.Parameters.Add(id);
SqlParameter data2 = new SqlParameter("@DATA", SqlDbType.VarChar, 8);data2.Value = FormataData();
cmd2.Parameters.Add(data2);
SqlParameter tipo2 = new SqlParameter("@ID_TIPO", SqlDbType.VarChar, 1);tipo2.Value = ddlTipo.SelectedItem.Value;
cmd2.Parameters.Add(tipo2);
SqlParameter status = new SqlParameter("@ID_STATUS", SqlDbType.VarChar, 1);status.Value = "C";cmd2.Parameters.Add(status);
SqlParameter idconsesubst = new SqlParameter("@ID_CONSE_SUBST", SqlDbType.Int, 4);idconsesubst.Value = Convert.DBNull;cmd2.Parameters.Add(idconsesubst);
SqlParameter justificativa = new SqlParameter("@JUSTIFICATIVA", SqlDbType.VarChar, 200);justificativa.Value = Convert.DBNull;cmd2.Parameters.Add(justificativa);
cmd2.ExecuteNonQuery();
}
Best regards,
Ivan Andrade
![]() |
0 |
![]() |
Some Points need to be cheked..
Is the dataReader returning any rows ?
Also you can't use the same connection for a query which is associated with a datareader already.
Satya
------------------------------
Mark as answer if you think
this helped you
![]() |
0 |
![]() |
If before cmd2.ExecuteNonQuery(); I close the connection:
dr.Close();
It works for the first record but after returning to the loop:
while
(dr.Read())it says that the connection is closed so that cannot continue...
How to do that?
using
System;using
System.Data;using
System.Data.SqlClient;using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;public
partial class REL_FREQ_CONSE : System.Web.UI.Page{
# region Page_Loadprotected void Page_Load(object sender, EventArgs e){
lblConfirma.Visible =
false;if (!IsPostBack){
txtData.Text = DataHoje();
// BindData();}
}
# endregion
# region Tratamento de datasstring DataHoje(){
string dia = DateTime.Now.Day.ToString();string mes = DateTime.Now.Month.ToString(); string ano = DateTime.Now.Year.ToString();if (dia.Length == 1) { dia = "0" + DateTime.Now.Day.ToString(); }if (mes.Length == 1) { mes = "0" + DateTime.Now.Month.ToString(); }return dia + "/" + mes + "/" + ano;
}
string FormataData(){
string dia = txtData.Text.Substring(0,2);string mes = txtData.Text.Substring(3,2); string ano = txtData.Text.Substring(6,4);if (dia.Length == 1) { dia = "0" + DateTime.Now.Day.ToString(); } if (mes.Length == 1) { mes = "0" + DateTime.Now.Month.ToString(); }return ano + mes + dia;}
# endregion
# region BindData - Popula DataGridvoid BindData(){
if (txtData.Text != string.Empty){
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["STRING_CONEXAO"]);SqlCommand cmd = new SqlCommand("SP_PRES_CONS_PRESENTES", conn);cmd.CommandType =
CommandType.StoredProcedure;SqlParameter tipo = new SqlParameter("@TIPO", SqlDbType.VarChar, 1);tipo.Value = ddlTipo.SelectedItem.Value;
cmd.Parameters.Add(tipo);
SqlParameter data = new SqlParameter("@DATA", SqlDbType.DateTime, 8);data.Value = Convert.ToDateTime(txtData.Text);cmd.Parameters.Add(data);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();if (dr.Read()){
dg.DataSource = dr;
dg.DataBind();
}
else{
dr.Close();
SqlCommand cmd2= new SqlCommand("SP_PRES_GRAVAR", conn);cmd2.CommandType =
CommandType.StoredProcedure;SqlCommand cmdnovo = new SqlCommand("SELECT CS_ID FROM Tbl_Conselheiros WHERE CS_TIPO IN (5,6,7)", conn);SqlDataReader drnovo = cmdnovo.ExecuteReader();while (drnovo.Read())
{
SqlParameter id = new SqlParameter("@ID_CONSE", SqlDbType.Int, 4);id.Value = Convert.ToInt32(drnovo[0]);cmd2.Parameters.Add(id);
SqlParameter data2 = new SqlParameter("@DATA", SqlDbType.VarChar, 8);data2.Value = FormataData();
cmd2.Parameters.Add(data2);
SqlParameter tipo2 = new SqlParameter("@ID_TIPO", SqlDbType.VarChar, 1);tipo2.Value = ddlTipo.SelectedItem.Value;
cmd2.Parameters.Add(tipo2);
SqlParameter status = new SqlParameter("@ID_STATUS", SqlDbType.VarChar, 1);status.Value = "C";cmd2.Parameters.Add(status);
SqlParameter idconsesubst = new SqlParameter("@ID_CONSE_SUBST", SqlDbType.Int, 4);idconsesubst.Value = Convert.DBNull;cmd2.Parameters.Add(idconsesubst);
SqlParameter justificativa = new SqlParameter("@JUSTIFICATIVA", SqlDbType.VarChar, 200);justificativa.Value = Convert.DBNull;cmd2.Parameters.Add(justificativa);
}
drnovo.Close();
cmd2.ExecuteNonQuery();
dr = cmd.ExecuteReader();
if (dr.Read()){
dg.DataSource = dr;
dg.DataBind();
}
}
conn.Close();
}
else { txtData.Focus(); }}
# endregion
# region
Eventos do DataGrid# region Editarprotected void dg_EditCommand(object source, DataGridCommandEventArgs e){
dg.EditItemIndex = e.Item.ItemIndex;
dg.Columns[1].Visible = true;BindData();
}
# endregion
# region Gravarprotected void dg_UpdateCommand(object source, DataGridCommandEventArgs e){
if (txtData.Text != string.Empty){
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["STRING_CONEXAO"]);SqlCommand cmd = new SqlCommand("SP_PRES_GRAVAR", conn); SqlCommand cmd2 = new SqlCommand("SP_PRES_LISTAR_CONSE_SUBST", conn);cmd.CommandType = CommandType.StoredProcedure;cmd2.CommandType =
CommandType.StoredProcedure;if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer && e.Item.ItemType != ListItemType.Pager){
string sStatus = null; DropDownList ddl3 = (DropDownList)e.Item.FindControl("ddlStatus");DropDownList ddl4 = (DropDownList)e.Item.FindControl("ddlConselheiros"); TextBox txt = (TextBox)e.Item.FindControl("txtStatus");Label lbl = (Label)e.Item.FindControl("lblId"); if (ddl3.SelectedItem.Value == "C") { sStatus = "C"; }if (ddl3.SelectedItem.Value == "J") { sStatus = "J"; } if (ddl3.SelectedItem.Value == "F") { sStatus = "F"; }if (ddl3.SelectedItem.Value == "L") { sStatus = "L"; } SqlParameter id = new SqlParameter("@ID_CONSE", SqlDbType.Int, 4);id.Value = Convert.ToInt32(lbl.Text);cmd.Parameters.Add(id);
SqlParameter data = new SqlParameter("@DATA", SqlDbType.VarChar, 8);data.Value = FormataData();
cmd.Parameters.Add(data);
SqlParameter tipo = new SqlParameter("@ID_TIPO", SqlDbType.VarChar, 1);tipo.Value = ddlTipo.SelectedItem.Value;
cmd.Parameters.Add(tipo);
SqlParameter status = new SqlParameter("@ID_STATUS", SqlDbType.VarChar, 1);status.Value = sStatus;
cmd.Parameters.Add(status);
SqlParameter subst = new SqlParameter("@ID_CONSE_SUBST", SqlDbType.Int, 4); bool substituto = true;if (sStatus == "C") { substituto = false; }if (ddl4.SelectedItem.Value != string.Empty && substituto == true){
subst.Value = Convert.ToInt32(ddl4.SelectedItem.Value);}
else { subst.Value = Convert.DBNull; }cmd.Parameters.Add(subst);
SqlParameter justif = new SqlParameter("@JUSTIFICATIVA", SqlDbType.VarChar, 200);if (txt.Text != "") { justif.Value = txt.Text; }else { justif.Value = Convert.DBNull; }cmd.Parameters.Add(justif);
// Parâmetros de verificação // Evita regravar um registro já existenteSqlParameter id_conse = new SqlParameter("@ID_CONSE", SqlDbType.Int, 4);id_conse.Value = Convert.ToInt32(lbl.Text);cmd2.Parameters.Add(id_conse);
SqlParameter data2 = new SqlParameter("@DATA", SqlDbType.VarChar, 8);data2.Value = FormataData();
cmd2.Parameters.Add(data2);
conn.Open();
SqlDataReader dr = cmd2.ExecuteReader();if (!dr.Read()){
cmd2.Connection.Close();
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
conn.Close();
cmd.Parameters.Clear();
cmd2.Parameters.Clear();
lblConfirma.Visible = true;lblConfirma.Text = "Inclusão efetuada com sucesso!";}
txtData.Text = DataHoje();
dg.EditItemIndex = -1;
dg.Columns[1].Visible = false;BindData();
}
}
# endregion
# region Cancelarprotected void dg_CancelCommand(object source, DataGridCommandEventArgs e){
dg.EditItemIndex = -1;
dg.Columns[1].Visible = false;BindData();
}
#endregion
# region Combo Statusprotected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e){
DropDownList ddl = (DropDownList)(sender);DropDownList ddl2 = (DropDownList)(ddl.Parent.Parent.FindControl("ddlStatus")); DropDownList ddl4 = (DropDownList)(ddl.Parent.Parent.FindControl("ddlConselheiros"));Panel ddl3 = (Panel)ddl.FindControl("panelStatus"); Panel pnl2 = (Panel)(ddl3.Parent.Parent.FindControl("panelStatus"));if (ddl2.Text != "C"){
pnl2.Visible = true;if (ddl4.Items[0].ToString() != string.Empty){
ddl4.Items.Insert(0,
new ListItem());ddl4.Items[1].Selected = false;ddl4.Items[0].Selected = true;}
}
else { pnl2.Visible = false; }}
# endregion
#endregion
Eventos do DataGrid#region Botão Carregarprotected void botCarregar_Click(object sender, EventArgs e){
BindData();
}
#endregion
}
Best regards,
Ivan Andrade
![]() |
0 |
![]() |
Hi,
I have not read your code but readin your problem i have understood that you are closing the connection after reading the first value in the loop,
you should not do this as datareader requires open connection if you close the connection sqldatareader can not be read so you have to keep the connection open until you have read the values from the datareader and close the connection outside the while loop.
this must solve your problem.
Known is handful and unknown is worldful.
Regards,
Ajay Matharu
http://ajaymatharu.com
![]() |
0 |
![]() |
Instead of passing one by one record to stored procedure, I would recommend pass the query as an argument to the stored procedure and execute it in the stored procedure..it will be easy and performance will also be gud
Pardeep Bogra (MCA, MCAD)
Systems Develper
![]() |
0 |
![]() |
simple solution is to create one more connection and use it to execute the sql command.. inside the loop
Satya
------------------------------
Mark as answer if you think
this helped you
![]() |
0 |
![]() |