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