在数据库中保存数据网格记录时出现问题 [英] Problem in saving datagrid records in database

查看:76
本文介绍了在数据库中保存数据网格记录时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人请帮助



我使用datagrid在数据库中保存多条记录。我在第一行插入数据,然后单击保存按钮,然后第一次在数据库中成功保存,但如果我关闭表格再次重新折叠并在第二行插入数据,则会出现一个错误...



错误---->>> {参数化查询'(@ id int,@ name nvarchar(6),@ breakfast nvarchar(4000),@ breturn nv'需要参数'@breakfast ',这是没有提供的。}



i使用下面的代码



Anyone please help

i am saving multiple records in database using datagrid . i insert data in first row and click on save button then first record save successfully in database but if i close form and recold again and insert data in second row then one error coming..

Error ---->>>{"The parameterized query '(@id int,@name nvarchar(6),@breakfast nvarchar(4000),@breturn nv' expects the parameter '@breakfast', which was not supplied."}

i am using below code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
protected SqlConnection conn;
protected SqlCommand cmd;

public Form4()
{
InitializeComponent();
}

private void Form4_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'testDataSet2.test1' table. You can move, or remove it, as needed.
this.test1TableAdapter.Fill(this.testDataSet2.test1);

}

private void button1_Click(object sender, EventArgs e)
{

for (int i = 0; i < g1.Rows.Count; i++)
{
conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Desktop\C# projects\WindowsFormsApplication3\WindowsFormsApplication3\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandText="INSERT INTO canteen (id,name,breakfast,breturn,lunch,lreturn,dinner,dreturn,brate,lrate,drate,total) values (@id,@name,@breakfast,@breturn,@lunch,@lreturn,@dinner,@dreturn,@brate,@lrate,@drate,@total)";
cmd.Parameters.AddWithValue("@id", g1.Rows[i].Cells[0].Value);
cmd.Parameters.AddWithValue("@name", g1.Rows[i].Cells[1].Value);
cmd.Parameters.AddWithValue("@breakfast", g1.Rows[i].Cells[2].Value);
cmd.Parameters.AddWithValue("@breturn", g1.Rows[i].Cells[3].Value);
cmd.Parameters.AddWithValue("@lunch", g1.Rows[i].Cells[4].Value);
cmd.Parameters.AddWithValue("@lreturn", g1.Rows[i].Cells[5].Value);
cmd.Parameters.AddWithValue("@dinner", g1.Rows[i].Cells[6].Value);
cmd.Parameters.AddWithValue("@dreturn", g1.Rows[i].Cells[7].Value);
cmd.Parameters.AddWithValue("@brate", g1.Rows[i].Cells[8].Value);
cmd.Parameters.AddWithValue("@lrate", g1.Rows[i].Cells[9].Value);
cmd.Parameters.AddWithValue("@drate", g1.Rows[i].Cells[10].Value);
cmd.Parameters.AddWithValue("@total", g1.Rows[i].Cells[11].Value);
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
//conn.Close();
MessageBox.Show("Added successfully!");

}

}


}
}

推荐答案

检查您的数据:我猜测第3列中至少有一行的值为null值:

Check your data: I'm guessing that at least one of your rows has a "null" value in column 3:
cmd.Parameters.AddWithValue("@breakfast", g1.Rows[i].Cells[2].Value);



并且您的SP未设置为接受空参数值。

你可以给你的SP参数默认为NULL - 在这种情况下它仍然可以工作。


And that your SP is not set up to accept null parameter values.
You could give your SP parameter a default of NULL - in which case it would work anyway.


这篇关于在数据库中保存数据网格记录时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆