我想使用ObjectDatasource从文本框中插入数据。ObjectDataSource绑定到网格视图,但仅显示某些计算列。文本框用于输入所有基本输入。ObjectDatasource的“删除和选择”命令(网格视图上的链接按钮)正在工作。然而,我在使用Insert命令时遇到了问题。我不知道如何将文本框中的数据作为参数传递给ObjectDataSource插入
编辑:使用下面的代码,将插入一条记录。正在传递参数。odssMain.Insert()给出错误:"Object reference not set to an object instance“。编辑:为什么我会得到这个错误?
此外,ObjectDataSource的行为也一直很奇怪。出现错误后,我必须在ODS向导上重新配置Insert方法,因为该方法将为空。
SQL3.5和ASP.NET 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();
}发布于 2011-10-24 02:05:24
你可以试着这样……
InsertParameter的ObjectDataSource如下所示
<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>我还将传递ObjectDataSource的InsertMethod属性,它将有一个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;//当您不传递任何值时,此检查是必要的,因为它将作为默认值传递,并将给出错误
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();
}
}按钮保存,用于插入记录。
//插入记录保存按钮
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函数将从相应列的脚注行获取TextBox文本值。
//获取GridView脚注行的TextBox值
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;
}
}和结果图像

是这样的..。
https://stackoverflow.com/questions/7863779
复制相似问题