2013-05-06 62 views
0

我已經在這個上搜索了幾天,並且發現了許多可能性,並且在C#代碼中進行了sql連接,但是我找不到任何使用像我的web.config和.aspx頁面。ASP.NET sqldatasource ReturnValue在代碼隱藏中無法訪問

問題:我有我的SQLdatasource1與插入使用存儲過程。 SP檢查一個重複的ID(MRN),如果它找到一個,吐出返回值0,如果沒有,吐出一個1。只要沒有重複,它也會執行插入操作。這一切都很好。

但是,我想抓住ReturnValue,檢查0或1並根據結果給用戶一條消息。這就是我正在處理的事情。爲簡單起見,它被縮寫。該表單是一個帶有InsertItemTemplate的asp:formview,並且所有控件都包含在其中。

<asp:sqldatasource ID="sqldatasource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:... %>" 
ProviderName="..." 
... 
<InsertParameters> 
    <asp:Parameter Name="return_value" DefaultValue="0" Type="Empty" Size="0" ConvertEmptyStringToNull="False" Direction="ReturnValue" /> 
</InsertParameters> 

在代碼隱藏中,我只是想抓住參數return_value的值並對其作出響應。我在下面是我想要做的,但顯然是錯誤的。我只是想將它張貼,因爲我想知道我的意圖。我嘗試過使用按鈕的單擊,並且這不僅給了我同樣的錯誤,即當前上下文中不存在return_value,但它也不會發布消息框。我已經嘗試了很多不同的東西,現在我迷了路。

更新:這是我目前在代碼隱藏。 MessageBox彈出時只顯示「@return_value」。

 protected void Sqldatasource1_Inserted(object sender, SqlDataSourceStatusEventArgs e) 
{ 
    SqlParameter return_value = new SqlParameter("@return_value", SqlDbType.Int); 
    return_value.Direction = ParameterDirection.ReturnValue; 
    return_value.Value = (object)return_value ?? DBNull.Value; 
    MessageBox.Show(return_value.Value.ToString()); 

    if (Convert.ToInt32(return_value.Value) != 1) 
    { 
     MessageBox.Show(return_value.Value.ToString()); 
    } 
    else 
    { 
     MessageBox.Show(return_value.Value.ToString()); 

    } 
} 

在此先感謝。對不起,如果這是可笑的基本,但我是盲目的。

UPDATE 2(2013年5月7日):經過額外的搜索和閱讀後,我嘗試了一些其他沒有奏效的東西。沒有錯誤,只是不起作用。我在插入的內容中嘗試了以下兩個位。

int? return_value = e.Command.Parameters["@return_value"].Value as int?; 
    MessageBox.Show(return_value.ToString()); 

int return_value = (Convert.ToInt32(e.Command.Parameters["@return_value"])); 
    MessageBox.Show(return_value.ToString()); 

回答

0

我已經回過頭來留下一個答案,因爲我討厭鬆散的結局。不幸的是,我永遠無法按照我的設想工作 - 在aspx文件技術中使用sqldatasource和模板。我嘗試了所有我能找到的東西,而沒有人爲我工作,很可能是因爲我對這個問題的無知,我錯過了一些東西。

我終於不得不回到開始,並在代碼隱藏中做所有事情。所以下面是整個事情減去我使用的其他40個參數。對於那些新來的或者只是兼職的ASP.NET C#dabblers的其他人,請注意您必須將數據,data.sqlclient和windows.forms添加到您的代碼隱藏中。您還需要添加Windows窗體參考(網站菜單/添加參考)。但請注意,您幾乎不希望將Windows.Forms用於警報。這是一個特例。以下是測試的最佳選擇,但要發佈給其他人使用,您需要使用Javascript或其他技術來提醒您。

祝你好運,我希望這可以幫助別人。

using System.Data; 
using System.Data.SqlClient; 
using System.Windows.Forms; 

...

protected void Save_Click(object sender, EventArgs e) 
{ 

    string connectionstring = WebConfigurationManager.ConnectionStrings["MyDB01"].ConnectionString; 
    SqlConnection con = new SqlConnection(); 
    con.ConnectionString = connectionstring; 
    SqlCommand cmd = new SqlCommand(); 

    cmd = new SqlCommand("DS_SSRS_Reports.dbo.DRPinsertPat", con); 


    cmd.Parameters.AddWithValue("@NPI", NPI.Text); 
    ... 
    cmd.Parameters.Add(new SqlParameter("@return_value", SqlDbType.Int)); 
    cmd.Parameters["@return_value"].Direction = ParameterDirection.ReturnValue; 

    cmd.CommandType = CommandType.StoredProcedure; 

    con.Open(); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 

    int r = (Convert.ToInt32(cmd.Parameters["@return_value"].Value)); 

    if (r.Equals(0)) 
    { 
     MessageBox.Show("MRN already exists in DRP database. Patient not entered."); 
    } 
    else 
    { 
     MessageBox.Show("Patient successfully added."); 
    } 
1

您是否嘗試過在插入時添加參數?看看這個鏈接:http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/7d37ac80-af6d-46ad-8ae0-dc7891596aac

標記的答案說,你可以添加一個「Return_Value」參數,將保存該值。

+0

感謝您的幫助。這似乎是朝着正確方向邁出的一步。我試圖改變爲C#,並做了OP爲他的解決方案做了什麼,但我想我得到的類型和轉換都搞砸了。我在那裏放置了一個消息框,試圖在IF之前看到return_value,但是整個事情都失敗了,所以我什麼都沒有。 'SqlParameter return_value = default(SqlParameter); return_value.Direction = ParameterDirection.ReturnValue; MessageBox.Show(return_value.Value.ToString()); if(Convert.ToInt32(return_value.Value)!= 1)...' – 2013-05-06 19:25:26

+0

我也嘗試過使用 'SqlParameter return_value = new SqlParameter(「@ return_value」,SqlDbType.Int);'而不是default( sqlparameter),但迄今爲止我還沒有更好的運氣。 – 2013-05-06 19:30:30

+0

你在檢查'插入'事件中的Return_value嗎?插入是在插入之前,插入將包含實際的返回值。 – AContractor 2013-05-06 19:33:47

1

對不起,長時間的延遲,認爲這將是記錄這一點,因爲我有同樣的問題有用的,不能在互聯網上找到任何答案。

要從SQL Server檢索返回值,您需要從Inserted事件訪問它。

protected void DSProjects_Inserted(object sender, SqlDataSourceStatusEventArgs e) 
     { 
      string strID = (e.Command.Parameters["@RETURN_VALUE"].Value).ToString(); 


      Response.Redirect("ProjectDetails.aspx?id=" + strID); 
     } 

幾件事情一提的是被忽視了:

  1. 返回值是不是在SQL選擇的值。在存儲過程,你需要使用「返回@ID」,而不是選擇@ID
  2. ASP參數應該是這樣的(確保大小不是默認爲0):

    <asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" Size="100" /> 
    

或者,您可以使用輸出SQL參數:

<asp:Parameter Direction="Output" Name="ID" Type="Int32" Size="100" /> 

並使用輸出設置SQL。在適用範圍:

@ID VARCHAR(20) OUTPUT 
代碼段

SET @ID = SCOPE_IDENTITY() 
+0

絕對是一分鐘,因爲我有這個問題,但我當然讚賞額外的輸入。我不知道什麼時候可能需要再次做這樣的事情,我會忘記我當時做錯的一切。無論哪種方式,謝謝!乾杯。 – 2017-03-14 23:53:40

相關問題