2011-10-23 29 views
2

我想使用ObjectDatasource從文本框中插入數據。 ObjectDataSource綁定到一個gridview,但只顯示某些計算列。文本框用於輸入所有基本輸入。 ObjectDatasource刪除&選擇命令(GridView上的鏈接按鈕)正在工作。不過,我在插入命令時遇到了問題。我無法弄清楚如何將文本框中的數據作爲參數傳遞給ObjectDataSource插入ObjectDatasource傳遞參數

編輯:使用下面的代碼,正在插入一條記錄。參數正在通過。 odssMain.Insert()給出錯誤:「未將對象引用設置爲對象的實例」。 編輯:爲什麼我得到這個錯誤?

此外,ObjectDataSource一直很奇怪。發生錯誤後,我必須在ODS嚮導中再次重新配置插入方法,因爲該方法將爲空。

ASP.NET 3.5 & SQL 2008,VS 2008

這裏是我的代碼:

<asp:ObjectDataSource ID="odsMain" runat="server" 
    SelectMethod="SelectMain" DeleteMethod="DeleteMain" 
    InsertMethod="InsertMain" UpdateMethod="UpdateMain" 
    OldValuesParameterFormatString="original_{0}" TypeName="MainDB" > 
....... 
    <InsertParameters> 
     <asp:Parameter Name="Quantity" Type="Int32" /> 
    </InsertParameters> 
DAL FILE: 
[DataObjectMethod(DataObjectMethodType.Insert)] 
public static int InsertMain(int Quantity)/ 
{ 
    SqlConnection con = new SqlConnection(GetConnectionString()); 

    string strQuery = "INSERT INTO t_Main (Quantity) VALUES (@Quantity)"; 
    SqlCommand cmd = new SqlCommand(strQuery, con); 
    cmd.Parameters.AddWithValue("@Quantity", Quantity); 

    con.Open(); 
    int i = cmd.ExecuteNonQuery(); 
    con.Close(); 

    return i; 
} 

CODE BEHIND FILE: 
protected void btnSaveAnalysis_Click(object sender, EventArgs e) 
{ 
    odsMain.InsertParameters.Clear(); 

    //Store parameters with values to the collection 
    odsMain.InsertParameters.Add(new Parameter ("Quantity", TypeCode.Int32, iQuantity.ToString())); 

    //Diferent ways that I tried. Still not working 
    //odsMain.InsertParameters.Add("Quantity", iQuantity.ToString()); 
    //odsMain.InsertParameters["Quantity"].DefaultValue = iQuantity.ToString(); 

    odsMain.Insert(); 

} 

回答

2

你可以嘗試這樣的....

ObjectDataSource控件用於InsertParameter看起來像一個下方

<InsertParameters> 
<asp:Parameter Name="FirstName" /> 
<asp:Parameter Name="MiddleName" /> 
<asp:Parameter Name="LastName" /> 
<asp:Parameter Name="Desgination" /> 
<asp:Parameter Name="Address" /> 
<asp:Parameter Name="City" /> 
<asp:Parameter Name="State" /> 
<asp:Parameter Name="Country" /> 
</InsertParameters> 

我也將通過InsertMethod本身ObjectDataSource的類型,它有一個InsertCustomer方法。

InsertCustomer方法看起來像下面的一個: -

public void InsertCustomer(string FirstName, string MiddleName,string LastName, string Desgination, string Address, string City, string State, string Country) 
    { 

    SqlConnection con = new SqlConnection(conStr); 
    SqlCommand cmd = new SqlCommand("InsertCustomer", con); 
    cmd.CommandType = CommandType.StoredProcedure; 

//這個檢查是必要的,當u不傳遞任何價值,因爲它會通過爲[默認],並給錯誤

if (string.IsNullOrEmpty(FirstName)) 
     FirstName = string.Empty; 
    if (string.IsNullOrEmpty(LastName)) 
     LastName = string.Empty; 
    if (string.IsNullOrEmpty(MiddleName)) 
     MiddleName = string.Empty; 

    if (string.IsNullOrEmpty(Desgination)) 
     Desgination = string.Empty; 

    if (string.IsNullOrEmpty(Address)) 
     Address = string.Empty; 

    if (string.IsNullOrEmpty(City)) 
     City = string.Empty; 

    if (string.IsNullOrEmpty(State)) 
     State = string.Empty; 

    if (string.IsNullOrEmpty(Country)) 
     Country = string.Empty; 

    cmd.Parameters.AddWithValue("@IV_FirstName", FirstName); 
    cmd.Parameters.AddWithValue("@IV_LastName", LastName); 
    cmd.Parameters.AddWithValue("@IV_MiddleName", MiddleName); 
    cmd.Parameters.AddWithValue("@IV_Desgination", Desgination); 
    cmd.Parameters.AddWithValue("@IV_Address", Address); 
    cmd.Parameters.AddWithValue("@IV_City", City); 
    cmd.Parameters.AddWithValue("@IV_State", State); 
    cmd.Parameters.AddWithValue("@IV_Country", Country); 
    using (con) 
    { 

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

    } 

    } 

Button保存插入記錄。

//插入記錄保存按鈕

protected void btnSave_Click(object sender, EventArgs e) 
{ 

    Customer.InsertParameters["FirstName"].DefaultValue = GetGridTextBoxValue("txtFirstName"); 
    Customer.InsertParameters["MiddleName"].DefaultValue = GetGridTextBoxValue("txtMiddleName"); 
    Customer.InsertParameters["LastName"].DefaultValue = GetGridTextBoxValue("txtLastName"); 
    Customer.InsertParameters["Desgination"].DefaultValue= GetGridTextBoxValue("txtDesgination"); 
    Customer.InsertParameters["Address"].DefaultValue = GetGridTextBoxValue("txtAddress"); 
    Customer.InsertParameters["City"].DefaultValue = GetGridTextBoxValue("txtCity"); 
    Customer.InsertParameters["State"].DefaultValue = GetGridTextBoxValue("txtState"); 
    Customer.InsertParameters["Country"].DefaultValue = GetGridTextBoxValue("txtCountry"); 
    Customer.Insert(); 

} 

GetGridTextBoxValue功能將得到來自各個欄尾行文本框的文本值。

//獲取的GridView的頁腳行

public string GetGridTextBoxValue(string txtID) 
{ 
    try 
    { 
     TextBox txt = (TextBox)gvCustomer.FooterRow.FindControl(txtID); // here you can place any text box value on your design page 
     return txt.Text; 

    } 

    catch (Exception ex) 
    { 
     return string.Empty; 
     throw ex; 
    } 

} 

文本框的值,結果圖像enter image description here是這樣的...

+0

是的,我沒有看到類似這樣的例子。 – user450143

+0

但是,Customer.InsertParameters [「FirstName」]。DefaultValue = strFirstName行給我一個運行時錯誤。 – user450143