开发者

using scope_Identity to get id to store in session variable

I need some help with my code. How can I get the scope_Identiy to store the ImageId as a session variable. I have been stuck on this for ages but I just cant get it to work.

edit: I believe that from the line SqlDataReader dataReader = cmd.ExecuteReader(); down is wrong. I just dont know how to fix it. Im still new to this.

protected void btnAddImage_Click(object sender, EventArgs e)
{

    //store items in session variables
    Session["AnimalName"] = AnimalNameTextBox.Text;          
    Session["TypeOfAnimal"] = TypeofAnimalDDL.Text;
    Session["Breed"] = BreedTextBox.Text;   
    Session["CrossBreed"] = CrossBreedAddDDL.Text;
    Session["Sex"] = SexDDL.Text;
    Session["Size"] = SizeDDL.Text;
    Session["Age"] = AgeDDL.Text;
    Session["Location"] = LocationAddDDL.Text;
    Session["Date"] = DateTextBox.Text;
    Session["Contact"] = ContactTextBox.Text;
    Session["Children"] = ChildrenDDL.Text;
    Session["OtherCats"] = OtherCatsDDL.Text;
    Session["OtherDogs"] = OtherDogsDDL.Text;
    Session["Neutered"] = NeuteredDDL.Text;
    Session["Microchipped"] = MicrochippedDDL.Text;
    Session["Colour"] = ColourTextBox.Text;
    Session["IndoorOutdoor"] = IndoorOutdoorDDL.Text;
    Session["Details"] = DetailsTextBox.Text;




      string s_Image_Name = txt_Image_Name.Text.ToString();
    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
    {
        int newImageID ;
        byte[] n_Image_Size = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile Posted_Image = FileUpload1.PostedFile;
        Posted_Image.InputStream.Read(n_Image_Size, 0, (int)FileUpload1.PostedFile.ContentLength);

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RescueAnimalsIrelandConnectionString"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO Images(Name,[Content],Size,Type) VALUES (@Image_Name,@Image_Content,@Image_Size,@Image_Type); SELECT ImageID = SCOPE_IDENTITY ()";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = conn;


        SqlParameter Image_Name = new SqlParameter("@Image_Name", SqlDbType.VarChar, 500);
        Image_Name.Value = txt_Image_Name.Text;
        cmd.Parameters.Add(Image_Name);

        SqlParameter Image_Content = new SqlParameter("@Image_Content", SqlDbType.Image, n_Image_Size.Length);
        Image_Content.Value = n_Image_Size;
        cmd.Parameters.Add(Image_Content);

        SqlParameter Image_Size = new SqlParameter("@Image_Size", SqlDbType.BigInt, 99999);
        Image_Size.Value = FileUpload1.PostedFile.ContentLength;
        cmd.Parameters.Add(Image_S开发者_开发知识库ize);

        SqlParameter Image_Type = new SqlParameter("@Image_Type", SqlDbType.VarChar, 500);
        Image_Type.Value = FileUpload1.PostedFile.ContentType;
        cmd.Parameters.Add(Image_Type);


        SqlDataReader dataReader = cmd.ExecuteReader();

        if (dataReader.HasRows)
        {
            dataReader.Read();
            newImageID = Convert.ToInt32(dataReader["ImageID"]);
        }

        dataReader.Close();

        }


Two suggestions:

First, do this in a stored procedure if possible. Return your new id as an output parameter

Second (and this may require some experimentation and spell checking - I haven't tried it), add an output parameter to your command and change the comamnd text slightly to get the value

SqlParameter Image_ID = new SqlParameter("@Image_ID", SqlDbType.Int);  
Image_ID.Direction = ParameterDirection.Output;
cmd.Parameters.Add(Image_ID); 

Change command text: SELECT @Image_ID = SCOPE_IDENTITY()

To read the result: int newId = Convert.ToInt32(cmd.Parameters["@Image_ID"].Value)


Try

Session["newImageID"] = Convert.ToInt32(dataReader["ImageID"]);

and when you want to retrieve newImageId, use

int newImageID = Session["newImageID"] as Int32;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜