Hi! I using this method for insert data into database it’s not worked. Can anybody help me?
CREATE TABLE [dbo].[TABDOLJ]
(
[IDDOL] [int] IDENTITY(1,1) NOT NULL,
[NAMED] [nvarchar](20) NOT NULL,
[SALARYB] [money] NOT NULL,
[SALARYS] [money] NOT NULL
)
private void DoljForm_Load(object sender, EventArgs e)
{
Grid.ColumnCount = 4;
Grid.Columns[0].Name = "№";
Grid.Columns[0].Width = 30;
Grid.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
Grid.Columns[1].Name = "Ноигӯи вазифаҳо";
Grid.Columns[1].Width = 200;
Grid.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
Grid.Columns[2].Name = "Маоши буҷет";
Grid.Columns[2].Width = 57;
Grid.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
Grid.Columns[3].Name = "Маоши махсус";
Grid.Columns[3].Width = 57;
Grid.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
private void btnSave_Click(object sender, EventArgs e)
{
jobs("del", "", 0, 0);
foreach (DataGridViewRow row in Grid.Rows)
{
jobs("add", row.Cells[1].Value.ToString(), Convert.ToDouble(row.Cells[2].Value), Convert.ToDouble(row.Cells[3].Value));
}
}
public void jobs(string command, string nm, double bj, double dg)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Salary"].ConnectionString);
SqlCommand cmd = con.CreateCommand();
string sql = "";
if (command == "add")
{
sql = "insert into tabdolj(named, salaryb, salarys)values( " + nm + "," + bj + "," + dg + ")";
}
if (command == "del")
{
sql = "truncate table tabdolj";
}
cmd.CommandText = sql;
con.Open();
try
{
cmd.ExecuteNonQuery();
}
catch { }
con.Close();
}
Name for example:
Rustam
Suhrob
Asror