2011-06-15 78 views
0

我需要一些帮助来处理我的代码。我如何获得scope_Identiy来将ImageId存储为会话变量。我一直坚持这个多年,但我不能得到它的工作。使用scope_Identity获取ID以存储在会话变量中

编辑:我相信从行SqlDataReader dataReader = cmd.ExecuteReader();下来是错误的。我只是不知道如何解决它。我对此仍然陌生。

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_Size); 

     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(); 

     } 
+0

不解决这一问题,但你可以使用'ExecuteScalar',而不是使用DataReader – Earlz 2011-06-15 19:51:08

+0

到底是什么错误/异常消息,你越来越多了?并且'newImageID'是一个类字段? – 2011-06-15 19:56:52

+0

@bala没有错误,它只是不工作。我一直在努力工作了很长时间,并且让它保存图像,但不知道如何让它给我保存的行的ID。谢谢 – RyanS 2011-06-15 20:18:39

回答

1

两个建议:

首先,在存储过程中,如果有可能做到这一点。返回你的新ID作为输出参数

其次(这可能需要一些实验和拼写检查 - 我没有尝试过),给你的命令添加一个输出参数并稍微改变文本文本以获得值

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

更改命令文本:SELECT @Image_ID = SCOPE_IDENTITY()

读结果:int newId = Convert.ToInt32(cmd.Parameters["@Image_ID"].Value)

+0

谢谢。不能得到它的工作。但认为它是我的部分代码而不是这个。 – RyanS 2011-06-15 20:21:28

+0

谢谢,我的新错误是一个ParameterName'Image_ID'的SqlParameter不包含在这个SqlParameterCollection中。我真的不知道我有什么错.. – RyanS 2011-06-15 21:20:46

+0

错字 - 使用“@Image_ID” - 答案固定 – Ray 2011-06-15 22:40:25

0

尝试

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

,当你想要检索newImageId,使用

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

即时通讯现在尝试。即时获取错误ExecuteReader需要一个开放和可用的连接。连接的当前状态已关闭。我错过了什么打开连接? – RyanS 2011-06-15 20:41:24

+0

使用“conn.open();” – Ray 2011-06-15 21:08:51