locked
ObjectDataSource Pass Parameters to Insert Command RRS feed

  • Question

  • User-1112440429 posted

    How do you pass parameters to the Insert command? Code shown below is not working. I get an error "Object reference not set to an instance of an object."

    Here's my code:

    asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    	SelectMethod="SelectMain" DeleteMethod="DeleteMain" 
    	InsertMethod="InsertMain" UpdateMethod="UpdateMain" 
    	OldValuesParameterFormatString="original_{0}" TypeName="MainDB" 
    	..................
    	<InsertParameters>
    		<asp:Parameter Name="iMyID" Type="Int32" />
    	</InsertParameters>
    </asp:ObjectDataSource>
    	
    [DataObjectMethod(DataObjectMethodType.Insert)]
    public static int InsertMain(int iMyID)
    {
    	SqlConnection con = new SqlConnection(GetConnectionString());
    	string strQuery = "INSERT INTO t_Main (MyID) VALUES (@MyID)";
    	SqlCommand cmd = new SqlCommand(strQuery, con);
    	
    	cmd.Parameters["@MyID"].Value = iMyID;
    	
    	con.Open();
    	int iAffectedRows = cmd.ExecuteNonQuery();
    	con.Close();
    	return iAffectedRows;
    }
    	
    protected void lbAdd_Click(object sender, EventArgs e)
    {
    	//Store parameters with values to the collection
    	ObjectDataSource1.InsertParameters.Add("MyID", TypeCode.Int32, iMyID.ToString());
    	
    	ObjectDataSource1.Insert();
    
    }
    Saturday, October 22, 2011 2:52 AM

Answers

  • User-1112440429 posted

    Got it to work.

    Changed

    ObjectDataSource1.InsertParameters.Add("MyID", TypeCode.Int32, iMyID.ToString());

    to

    ObjectDataSource1.InsertParameters.Add("MyID", iMyID.ToString());

    ObjectDataSource1.InsertParameters["iMyID"].DefaultValue = Your Value here gives a compile error

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 7, 2011 10:36 AM

All replies

  • User3866881 posted

    Hello:)

    Please do the following things to solve your problem:

    1) [DataObjectMethod(DataObjectMethodType.Insert)]——Please remove it, because it's only a single type int instead of a compond time like a class or struct.

    2)

    //Store parameters with values to the collection ObjectDataSource1.InsertParameters.Add("MyID", TypeCode.Int32, iMyID.ToString());

    Since you've defined the insert parameter inside the ObjectDataSource, so you don't need to assign it again, But just have a try like this:

    ObjectDataSource1.InsertParameters["iMyID"].DefaultValue = Your Value here;

    ObjectDataSource1.Insert();

    Sunday, October 23, 2011 9:56 PM
  • User-1112440429 posted

    Got it to work.

    Changed

    ObjectDataSource1.InsertParameters.Add("MyID", TypeCode.Int32, iMyID.ToString());

    to

    ObjectDataSource1.InsertParameters.Add("MyID", iMyID.ToString());

    ObjectDataSource1.InsertParameters["iMyID"].DefaultValue = Your Value here gives a compile error

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 7, 2011 10:36 AM