Click here to Skip to main content
15,896,408 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Try to update data from DataGridView using SqlDataAdapter but getting following error when Update(Save) button click

The DataAdapter.SelectCommand property needs to be initialized.

What I have tried:

My code on Load and Save button

C#
SqlDataAdapter adp;
SqlCommandBuilder cmdBld;
DataTable dtEditMember = new DataTable();

private void butLoadMember_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(dbConn))
    {
        conn.Open();
        using (adp = new SqlDataAdapter())
        {
            if (txtEditMemberVehiNo.MaskCompleted)
            {
                adp.SelectCommand = new SqlCommand("Select * From Master Where VehiNo = @VehiNo");
                adp.SelectCommand.Parameters.Add("@VehiNo", SqlDbType.NVarChar).Value = txtEditMemberVehiNo.Text;
            }
            else if (!string.IsNullOrWhiteSpace(txtEditMemberCustName.Text))
            {
                adp.SelectCommand = new SqlCommand("Select * From Master Where CustName = @CustName");
                adp.SelectCommand.Parameters.Add("@CustName", SqlDbType.NVarChar).Value = txtEditMemberCustName.Text;
            }

            adp.SelectCommand.Connection = conn;
            adp.Fill(dtEditMember);
        }
    }
    if (dtEditMember.Rows.Count > 0)
    {
        dgvEditMember.DataSource = dtEditMember;
        dgvEditMember.Columns["VehiNo"].HeaderText = "Vehicle No";

        butLoadMember.Enabled = false;
        butUpdateMember.Enabled = true;
    }
    else
    {
        txtEditMemberVehiNo.Clear();
        txtEditMemberCustName.Clear();
        dgvEditMember.Columns.Clear();
        MessageBox.Show("Member Not Found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

}

private void butUpdateMember_Click(object sender, EventArgs e)
{

    cmdBld = new SqlCommandBuilder(adp);
    if (dtEditMember.GetChanges() != null)
    {
        adp.Update(dtEditMember);
        MessageBox.Show("Changes Done.");
    }
}
Posted
Comments
sameer549 27-Mar-17 6:42am    
check this link once,
http://www.c-sharpcorner.com/uploadfile/61b832/sqldataadapter-update-method/
CHill60 27-Mar-17 6:58am    
If this error is raised within the butUpdateMember_Click then it's because you haven't initialized anything other than the Command builder ...look at all the code in butLoadMember_Click ...
AjayBaroda 28-Mar-17 0:51am    
i tried adding update statement in butLoadMember_Click and also tried in butUpdateMember_Click but none is working. Please specify best solution hint. Like where and what update statement i should include.
sameer549 28-Mar-17 5:04am    
i think in butUpdateMember_Click, you are missing update command before line, adp.Update(dtEditMember)
it should be something like
//select the update command
adp.UpdateCommand=cmd;
//update the data source
adp.Update(dtEditMember);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900