Usuário com melhor resposta
Como coloco um parameter bool corretamente num SqlDataAdapter para evitar SqlInjection

Pergunta
-
Olá pessoal tenho este codigo em CSharp Asp.Net no VS2008 pro Edition
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.Data.SqlClient; using System.Web.Configuration; public partial class testegridview : System.Web.UI.Page { private string SqlConnectionName = "LocalSqlServer"; SqlDataAdapter daMySQL; DataSet dsMySQL; string strSQL; protected void Page_Load(object sender, EventArgs e) { //Dados de Conexão á Base de Dados SQL SqlConnection connection = default(SqlConnection); connection = new SqlConnection(WebConfigurationManager.ConnectionStrings[String.Format("{0}", SqlConnectionName)].ConnectionString); //conexaoMySQL = new OdbcConnection(conStr); strSQL = "SELECT IDNoticia,ImgPeq,Titulo,Resenha,DataNoticia,HorarioNoticia FROM Noticias Where AutorizaNoticia = @AutorizaNoticia"; SqlCommand Command = new SqlCommand(strSQL, connection); Command.Parameters.AddWithValue("@AutorizaNoticia", Convert.ToBoolean(1)); daMySQL = new SqlDataAdapter(strSQL, connection); dsMySQL = new DataSet(); daMySQL.Fill(dsMySQL, "Noticias"); // dsMySQL.Columns["Resumo"].Expression = "substring(Resenha,1,60)"; dgMySQL.DataSource = dsMySQL; dgMySQL.DataBind(); } protected void dgMySQL_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label lb = e.Row.Cells[2].FindControl("Resenha") as Label; string desc = lb.Text.Trim(); int size = desc.Length; if (size > 10) { desc = desc.Remove(10); int position = desc.LastIndexOf(" "); desc = desc.Remove(position); } lb.Text = desc.ToString() + " ..."; } } }
Só que ao executar o asp.net website ele tá dando erro de execute escalar
enfim como coloco corretamente o comando de parameters no SqlDataAdapter para receber o valor true do AutorizaNoticia
Se alguem puder me ajudar desde já agradeço
LADEF
Respostas
-
Olá pessoal tenho este codigo em CSharp Asp.Net no VS2008 pro Edition
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.Data.SqlClient; using System.Web.Configuration; public partial class testegridview : System.Web.UI.Page { private string SqlConnectionName = "LocalSqlServer"; SqlDataAdapter daMySQL; DataSet dsMySQL; string strSQL; protected void Page_Load(object sender, EventArgs e) { //Dados de Conexão á Base de Dados SQL SqlConnection connection = default(SqlConnection); connection = new SqlConnection(WebConfigurationManager.ConnectionStrings[String.Format("{0}", SqlConnectionName)].ConnectionString); //conexaoMySQL = new OdbcConnection(conStr); strSQL = "SELECT IDNoticia,ImgPeq,Titulo,Resenha,DataNoticia,HorarioNoticia FROM Noticias Where AutorizaNoticia = @AutorizaNoticia"; SqlCommand Command = new SqlCommand(strSQL, connection); Command.Parameters.AddWithValue("@AutorizaNoticia", Convert.ToBoolean(1)); daMySQL = new SqlDataAdapter(strSQL, connection); dsMySQL = new DataSet(); daMySQL.Fill(dsMySQL, "Noticias"); // dsMySQL.Columns["Resumo"].Expression = "substring(Resenha,1,60)"; dgMySQL.DataSource = dsMySQL; dgMySQL.DataBind(); } protected void dgMySQL_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label lb = e.Row.Cells[2].FindControl("Resenha") as Label; string desc = lb.Text.Trim(); int size = desc.Length; if (size > 10) { desc = desc.Remove(10); int position = desc.LastIndexOf(" "); desc = desc.Remove(position); } lb.Text = desc.ToString() + " ..."; } } }
Só que ao executar o asp.net website ele tá dando erro de execute escalar
enfim como coloco corretamente o comando de parameters no SqlDataAdapter para receber o valor true do AutorizaNoticia
Se alguem puder me ajudar desde já agradeço
LADEF
Certeza de que o erro não é por causa do boolean. E como o Danimar disse, você pode passar direto o valor "True" e não converter a partir do número 1.
Eu vi que você esquecer de dizer que seu comando é do tipo texto, e isso que vai dar erro. Coloque:
Command.CommandText = CommandType.Text;
antes de executar o comando
- Marcado como Resposta LuizIta sexta-feira, 7 de dezembro de 2012 03:24
-
Tem certeza que o erro é por causa do boolean?
Command.Parameters.AddWithValue("@AutorizaNoticia", True);
Não precisa converter 1 para booleano, usa diretamente True;
Eu geralmente uso desta forma aqui, especificando o tipo do parâmetro.
comando.Parameters.Add(
"@Parametro", NpgsqlTypes.NpgsqlDbType.Boolean).Value = True;
- Marcado como Resposta LuizIta sexta-feira, 7 de dezembro de 2012 03:24
Todas as Respostas
-
Tem certeza que o erro é por causa do boolean?
Command.Parameters.AddWithValue("@AutorizaNoticia", True);
Não precisa converter 1 para booleano, usa diretamente True;
Eu geralmente uso desta forma aqui, especificando o tipo do parâmetro.
comando.Parameters.Add(
"@Parametro", NpgsqlTypes.NpgsqlDbType.Boolean).Value = True;
- Marcado como Resposta LuizIta sexta-feira, 7 de dezembro de 2012 03:24
-
Olá pessoal tenho este codigo em CSharp Asp.Net no VS2008 pro Edition
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.Data.SqlClient; using System.Web.Configuration; public partial class testegridview : System.Web.UI.Page { private string SqlConnectionName = "LocalSqlServer"; SqlDataAdapter daMySQL; DataSet dsMySQL; string strSQL; protected void Page_Load(object sender, EventArgs e) { //Dados de Conexão á Base de Dados SQL SqlConnection connection = default(SqlConnection); connection = new SqlConnection(WebConfigurationManager.ConnectionStrings[String.Format("{0}", SqlConnectionName)].ConnectionString); //conexaoMySQL = new OdbcConnection(conStr); strSQL = "SELECT IDNoticia,ImgPeq,Titulo,Resenha,DataNoticia,HorarioNoticia FROM Noticias Where AutorizaNoticia = @AutorizaNoticia"; SqlCommand Command = new SqlCommand(strSQL, connection); Command.Parameters.AddWithValue("@AutorizaNoticia", Convert.ToBoolean(1)); daMySQL = new SqlDataAdapter(strSQL, connection); dsMySQL = new DataSet(); daMySQL.Fill(dsMySQL, "Noticias"); // dsMySQL.Columns["Resumo"].Expression = "substring(Resenha,1,60)"; dgMySQL.DataSource = dsMySQL; dgMySQL.DataBind(); } protected void dgMySQL_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label lb = e.Row.Cells[2].FindControl("Resenha") as Label; string desc = lb.Text.Trim(); int size = desc.Length; if (size > 10) { desc = desc.Remove(10); int position = desc.LastIndexOf(" "); desc = desc.Remove(position); } lb.Text = desc.ToString() + " ..."; } } }
Só que ao executar o asp.net website ele tá dando erro de execute escalar
enfim como coloco corretamente o comando de parameters no SqlDataAdapter para receber o valor true do AutorizaNoticia
Se alguem puder me ajudar desde já agradeço
LADEF
Certeza de que o erro não é por causa do boolean. E como o Danimar disse, você pode passar direto o valor "True" e não converter a partir do número 1.
Eu vi que você esquecer de dizer que seu comando é do tipo texto, e isso que vai dar erro. Coloque:
Command.CommandText = CommandType.Text;
antes de executar o comando
- Marcado como Resposta LuizIta sexta-feira, 7 de dezembro de 2012 03:24