3

Buen día, estoy tratando de enviar el arreglo sn a SQL SERVER pero me sale el error

The parameterized query '(@Serial_Number nvarchar(4000))INSERT INTO Auditorias (Serial_Nu' expects the parameter '@Serial_Number', which was not supplied.

Adjunto el código, se manda cuando se hace clic en el botón "Approve":

public partial class Audit : System.Web.UI.Page
{
    List<string> Serial_Number = new List<string>();
    List<string> User_SSO = new List<string>();
    List<string> User_Name = new List<string>();
    string[] sn = new string[5];
    string[] sso = new string[5];
    string[] user = new string[5];

    int i = 0;
    public void Page_Load(object sender, EventArgs e)
    {
        StringBuilder table = new StringBuilder();

        if (!Page.IsPostBack)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ITTESTDBConnectionString"].ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT TOP 5 Brand, Model, Service_Tag, Hostname, User_SSO, User_Name, Memory, HD, OS FROM Laptops ORDER BY NEWID()";
            cmd.Connection = con;
            SqlDataReader rd = cmd.ExecuteReader();
            table.Append("<table border='1'>");
            table.Append("<tr><th>Brand</th><th>Model</th><th>Service Tag</th><th>Hostname</th><th>User SSO</th><th>User Name</th><th>Memory</th><th>HDD</th><th>Operative System</th>");
            table.Append("</tr>");

            if (rd.HasRows)
            {
                while (rd.Read())
                {
                    table.Append("<tr>");
                    table.Append("<td>" + rd[0] + "</td>");
                    table.Append("<td>" + rd[1] + "</td>");
                    table.Append("<td>" + rd[2] + "</td>");
                    table.Append("<td>" + rd[3] + "</td>");
                    table.Append("<td>" + rd[4] + "</td>");
                    table.Append("<td>" + rd[5] + "</td>");
                    table.Append("<td>" + rd[6] + "</td>");
                    table.Append("<td>" + rd[7] + "</td>");
                    table.Append("<td>" + rd[8] + "</td>");
                    Serial_Number.Add(rd[2].ToString());
                }
            }

            string[] sn = Serial_Number.ToArray();
            table.Append("</table>");
            PlaceHolder1.Controls.Add(new Literal { Text = table.ToString() });
            rd.Close();
        }
    }


    public void btnApprove_Click(object sender, EventArgs e)
    {

        for (i=0; i<5; i++)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ITTESTDBConnectionString"].ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO Auditorias (Serial_Number, Estado) VALUES (@Serial_Number,'Correcto')";
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Serial_Number", sn[i]);

            cmd.ExecuteNonQuery();
        }
    }
}

1 respuesta 1

4

Recuerda que estas en un ambiente web, por lo tanto los valores de las variables no se conservan porque no hay estado entre cada request, para eso existe el objeto Session

Analiza algo como esto:

public partial class Audit : System.Web.UI.Page
{

    public void Page_Load(object sender, EventArgs e)
    {
        List<string> Serial_Number = new List<string>();

        StringBuilder table = new StringBuilder();

        if (!Page.IsPostBack)
        {
            sting connstring = ConfigurationManager.ConnectionStrings["ITTESTDBConnectionString"].ToString();
            using(SqlConnection con = new SqlConnection(connstring))
            {
                con.Open();

                string query = @"SELECT TOP 5 Brand, Model, Service_Tag, Hostname, User_SSO, User_Name, Memory, HD, OS 
                                FROM Laptops ORDER BY NEWID()";
                SqlCommand cmd = new SqlCommand(query, con);

                SqlDataReader rd = cmd.ExecuteReader();

                table.Append("<table border='1'>");
                table.Append("<tr><th>Brand</th><th>Model</th><th>Service Tag</th><th>Hostname</th><th>User SSO</th><th>User Name</th><th>Memory</th><th>HDD</th><th>Operative System</th>");
                table.Append("</tr>");

                if (rd.HasRows)
                {
                    while (rd.Read())
                    {
                        table.Append("<tr>");
                        table.Append("<td>" + rd[0] + "</td>");
                        table.Append("<td>" + rd[1] + "</td>");
                        table.Append("<td>" + rd[2] + "</td>");
                        table.Append("<td>" + rd[3] + "</td>");
                        table.Append("<td>" + rd[4] + "</td>");
                        table.Append("<td>" + rd[5] + "</td>");
                        table.Append("<td>" + rd[6] + "</td>");
                        table.Append("<td>" + rd[7] + "</td>");
                        table.Append("<td>" + rd[8] + "</td>");
                        Serial_Number.Add(rd[2].ToString());
                    }
                }

                table.Append("</table>");

                Session["Serial_Number"] = Serial_Number;

                PlaceHolder1.Controls.Add(new Literal { Text = table.ToString() });
           }
        }
    }


    public void btnApprove_Click(object sender, EventArgs e)
    {

        List<string> serialNumber = (List<string>)Session["Serial_Number"];

        string connstring = ConfigurationManager.ConnectionStrings["ITTESTDBConnectionString"].ToString();
        using(SqlConnection con = new SqlConnection(connstring))
        {
            con.Open();
            string query = "INSERT INTO Auditorias (Serial_Number, Estado) VALUES (@SerialNumber, 'Correcto')";
            SqlCommand cmd = new SqlCommand(query, con);

            foreach(var sn in serialNumber)
            {
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@SerialNumber", sn);

                cmd.ExecuteNonQuery();
            }
        }

    }
}

Veras como uso:

Session["Serial_Number"] = Serial_Number;

para conservar la lista de serial number entre request, para después poder recuperarla

List<string> serialNumber = (List<string>)Session["Serial_Number"];

iterando los valores que serán pasado en el parámetro del insert

1
  • Eres un crack amigo, muchas gracias, habia pasado por alto el hecho de que no se conservan los valores Commented el 22 feb. 2019 a las 16:31

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.