Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6725419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:48:18+00:00 2026-05-26T09:48:18+00:00

I want to Insert data from textboxes using ObjectDatasource. The ObjectDataSource is bound to

  • 0

I want to Insert data from textboxes using ObjectDatasource. The ObjectDataSource is bound to a gridview but displays certain computed columns only. The Textboxes are used to input all the basic inputs.
ObjectDatasource Delete & Select commands (Link buttons on gridview) are working. However I am having trouble with Insert command. I am not able to figure out how to pass the data from the textboxes as parameters to the ObjectDataSource Insert

EDIT: With the code below, a record is getting inserted. Parameters are getting passed. odssMain.Insert() gives the Error: “Object reference not set to an instance of an object”.
EDIT: WHY AM I GETTING THIS ERROR?

Also the ObjectDataSource has been acting weird. After an error, I have to reconfigure the Insert Method again on the ODS Wizard as the method will be blank.

ASP.NET 3.5 & SQL 2008, VS 2008.

Here’s my code:

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:48:18+00:00Added an answer on May 26, 2026 at 9:48 am

    you could try like this….

    ObjectDataSource for InsertParameter looks like below one

    <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>
    

    I will also pass InsertMethod property of ObjectDataSource,which will have an InsertCustomer method.

    InsertCustomer method looks like below one :-

       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;
    

    //this check is necessary, when u don’t pass any value as it will pass as [default] and will give error

        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 Save for inserting record.

    //Insert record Save 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 function will get TextBox text value from footer row of respective column.

    //Get TextBox value of GridView Footer Row

    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;  
        }  
    
    }
    

    and the results image enter image description here is like this …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to insert data from a table with an identity
I want to insert something into a STL list in C++, but I only
i want to insert data from an excel file into a local database in
I am copying data from MS Access to SQL Server using SSIS. Only one
I am using xml-rpc to insert data from a desktop app to a webserver
I am using an SQL query to insert data from an excel worksheet into
I want to insert data using XML in SQL Server 2005. So I got
i retrieve data from upc database and then want to insert the data into
I want to insert data from one DataTable to another with some conditions. I
I want to insert data in MYSQL database using python here my code import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.