I have problem when saving data from gridview into SQL database.
When i save the data, only certain data was saved. I want to save all the data. Data that was not saved is from the column where i display a drop down list while editing from the gridview.

Another problem is when there is no data in certain column in the grid view and when i click the SAVE button, " " is saved into the database. How to make sure that " " is not saved into the table when there is no data in the gridview?

Here is my coding to save data from grid view when user make selection and click SAVE button (Please help me to solve my problem):

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace BDAS
{
    public partial class ViewAllProposal : System.Web.UI.Page
    {
        DBConn myDB = new DBConn();
        SqlConnection SQLConn = new SqlConnection();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex != -1)
            {
                string proposal_no;
                string project_name;
                string company_name;
                string start_date;
                string end_date;
                string duration;
                string revenue;
                string OM;
                string status_proposal;
                string vendor_name;
                string remarks;

                proposal_no = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text.Trim();
                project_name = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text.Trim();
                company_name = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text.Trim();
                start_date = GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text.Trim();
                end_date = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text.Trim();
                duration = GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text.Trim();
                revenue = GridView1.Rows[GridView1.SelectedIndex].Cells[7].Text.Trim();
                OM = GridView1.Rows[GridView1.SelectedIndex].Cells[8].Text.Trim();
                status_proposal = GridView1.Rows[GridView1.SelectedIndex].Cells[9].Text.Trim();
                vendor_name = GridView1.Rows[GridView1.SelectedIndex].Cells[10].Text.Trim();
                remarks = GridView1.Rows[GridView1.SelectedIndex].Cells[11].Text.Trim();
                
                try
                {
                    SQLConn = myDB.OpenDB();

                    string insertString = @"insert into Proposal_history values ('" + proposal_no +
                     "', '" + project_name +
                     "', '" + company_name +
                     "', '" + start_date +
                     "', '" + end_date +
                     "', '" + duration +
                     "', '" + revenue +
                     "', '" + OM +
                     "', '" + status_proposal +
                     "', '" + vendor_name +
                     "', '" + remarks + "')";

                    SqlCommand cmd = new SqlCommand(insertString, SQLConn);
                    int returnValue = cmd.ExecuteNonQuery();

                    if (returnValue > 0)
                    {
                        Response.Write("<script>alert('Data saved...')</script>");
                    }
                }

                catch (Exception ex)
                {
                    Response.Write("<script>alert('" + "ERROR!!!" + "')</script>");
                }

                finally
                {
                    myDB.CloseDB();
                }
            }
        }
    }
}

Recommended Answers

All 5 Replies

wot's the error..?
Did u catch any exception..
?

wot's the error..?
Did u catch any exception..
?

There is no error that occur. It can save the data. But, not the data from the column where i display the drop down list when editing from the gridview.

Please somebody help me to solve my problem...
How to save data from user selection in gridview into certain table in SQL database. I have problem on saving the data.

show me your complete aspx and .cs code
may be i can fix it

Please somebody help me to solve my problem...
How to save data from user selection in gridview into certain table in SQL database. I have problem on saving the data.

I guess you need to do something like
DropDownList ddlTest = (DropDownList)GridView1.Rows[GridView1.SelectedIndex].FindContro("your Dropdown id")

then use ddlTest.Text or other property and assign it.
I hope it may solve your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.