变量名'@KorisnickoIme'已经被声明。变量名在查询批处理或存储过程中必须是唯一的

时间:2013-03-10 12:15:07

标签: c# sql

我尝试从GridView中对您收到错误的数据进行排序(启用排序)。可变名称在查询批处理或存储过程中必须是唯一的。 2.这里和适配器

{
 istorijaKupovinaDataSource.SelectCommand = @"
       SELECT   k.Datum, p.Naziv AS Proizvodjac, l.Naziv AS Lek, k.Kolicina, k.Cena, ni.Naziv AS NacinIsporuke
            FROM    Kupovina k
                    INNER JOIN Lek l ON k.LekId = l.Id 
                    INNER JOIN NacinIsporuke ni ON k.NacinIsporukeID = ni.Id 
                    INNER JOIN Proizvodjac p ON l.ProizvodjacId = p.Id 
                    INNER JOIN Kupac u ON k.KupacId = u.Id
            WHERE   u.KorisnickoIme = @KorisnickoIme
            ORDER BY k.Datum";

        istorijaKupovinaDataSource.SelectParameters.Add("KorisnickoIme", DbType.String, User.Identity.Name);

}
public static void Kupi(long lekID, int kolicina, decimal cena, long nacinIsporukeID, string korisnickoIme)
    {
        SqlConnection con = new SqlConnection();
        try
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["OnlineApotekaConnectionString"].ConnectionString;
            con.Open();

            string updateLager = @"
            UPDATE  Lager
            SET Kolicina=Kolicina-@Kolicina
            WHERE LekID=@LekID";

            SqlCommand cmd = new SqlCommand(updateLager, con);
            cmd.Parameters.AddWithValue("@LekID", lekID);
            cmd.Parameters.AddWithValue("@Kolicina", kolicina);

            cmd.ExecuteNonQuery();

            string insertIntoKupovina=@"
                INSERT INTO Kupovina (KupacID, LekID, Datum, Kolicina, Cena, NacinIsporukeID) 
                VALUES (@KupacID, @LekID, @Datum, @Kolicina, @Cena, @NacinIsporukeID)";

            cmd = new SqlCommand(insertIntoKupovina, con);
            cmd.Parameters.AddWithValue("@KupacID", KupacAdapter.GetID(korisnickoIme));
            cmd.Parameters.AddWithValue("@LekID", lekID);
            cmd.Parameters.AddWithValue("@Datum", DateTime.Now.Date);
            cmd.Parameters.AddWithValue("@Kolicina", kolicina);
            cmd.Parameters.AddWithValue("@Cena", cena);
            cmd.Parameters.AddWithValue("@NacinIsporukeID", nacinIsporukeID);

            cmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }
}

{         SqlConnection con = new SqlConnection();

    try
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["OnlineApotekaConnectionString"].ConnectionString;
        con.Open();

        string selectQuery = @"
            SELECT ID
            FROM Kupac
            WHERE KorisnickoIme = @KorisnickoIme";

        SqlCommand cmd = new SqlCommand(selectQuery, con);
        cmd.Parameters.AddWithValue("@KorisnickoIme", korisnickoIme);

        return Convert.ToInt64(cmd.ExecuteScalar());

    }
    catch (Exception err)
    {
        throw err;
    }
    finally
    {
        con.Close();
    }
}
public List<Kupac> SelectAll()
{
    List<Kupac> list = new List<Kupac>();

    SqlConnection con = new SqlConnection();

    try
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["OnlineAotekaConnectionString"].ConnectionString;
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;

        string selectQuery = "SELECT * FROM Kupac";

        cmd.CommandText = selectQuery;
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            list.Add(new Kupac(reader["Jmbg"].ToString(), reader["Ime"].ToString(), reader["Prezime"].ToString(), reader["Telefon"].ToString(), reader["Adresa"].ToString(), reader["Email"].ToString(), reader["KorisnickoIme"].ToString()));
        }
        reader.Close();
        return list;
    }
    catch (Exception err)
    {
        throw err;
    }
    finally
    {
        con.Close();
    }
}

}

1 个答案:

答案 0 :(得分:4)

我认为你应该使用

  istorijaKupovinaDataSource.SelectParameters.Clear();
    istorijaKupovinaDataSource.SelectParameters.Add("KorisnickoIme", DbType.String, User.Identity.Name);

而不是

istorijaKupovinaDataSource.SelectParameters.Add("KorisnickoIme", DbType.String, User.Identity.Name);