PROBLEM WITH SQL QUERY IN C # ASP.NET

1

I have a webform project in asp .net with c # and in my query sql I need data that the user will provide.

The week field is entered in a textbox in the webform1 and the query is in the webform2. According to that data, it will show me certain records in a gridview. In addition to the session start cod_user. Below I show the code

Errors:

  

* 'The value can not be null. Parameter name: String 'of the line:   int value = Int32.Parse (Request.QueryString ["week"]);

* 'Query with parameters' (@cod varchar (5)) SELECT blah bla 'awaits the parameter' @cod ', which has not been provided.'

PAGE ORIGIN

   protected void BUTTON1_Click(object sender, EventArgs e)
   {
       string semana = sem.Text;
       if (sem.Text != null)
       {
         Response.Redirect("webform2.aspx?semana=" + semana);
       }
       else {
           Response.Redirect("webform1.aspx");
       }
   }

PAGE DESTINY

protectedvoid Page_Load(object sender, EventArgs e)
    {
        int valor = Int32.Parse(Request.QueryString["semana"]);// valor que viene de otro webform
       SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["cn"].ToString());

        con.Open();
            SqlCommand cmd = newSqlCommand("SELECT WHERE(Sem.Semana = '@sem') AND(Cab.Cod_user= '@cod') AND(etc...", con);
         cmd.Parameters.Add("@sem", SqlDbType.TinyInt, 3);
        cmd.Parameters["@sem"].Value = valor;
       cmd.Parameters.Add("@cod", SqlDbType.VarChar, 5);

cmd.Parameters["@cod"].Value = Session["Cod_user"]; 

SqlDataAdapter da = newSqlDataAdapter(cmd);
            DataTable dt = newDataTable();
            da.Fill(dt);
           gridview1.Visible = true;
           gridview1.DataSource = dt;
           gridview.DataBind();
         con.Close();
        }           

I hope you can help me

Greetings

Thanks

    
asked by sunflower 08.03.2018 в 20:06
source

1 answer

1

We go by parts, verify that you have data with the following lines of code

var requestSemana = Request.QueryString["semana"];
var laSessionActual = Session["Cod_user"]; 
int valor = Int32.Parse(Request.QueryString["semana"]); //Aquí coloca un breakpoint.
//De esta manera podrás ver la información de las 2 variables anteriores.

Update

For the Cod_User, replace:

cmd.Parameters["@cod"].Value = Session["Cod_user"]; 

By:

cmd.Parameters["@cod"].Value = (string)Session["Cod_user"]; 

For the week:

Replaces:

int valor = Int32.Parse(Request.QueryString["semana"]);

By:

int valor = Convert.ToInt32(Request.QueryString["semana"]);
    
answered by 08.03.2018 / 20:32
source