Atul Rokade

Atul Rokade

  • NA
  • 141
  • 43.8k

how to add datagridview detail in access table

May 18 2016 3:07 PM

I'm creating a medical store application, with textboxes and one datagridview (see image below of my form). When a user clicks the add button, I want all textbox values and datagridview values to be inserted into database. Also I want from particular reg no.(which is primary key) patient name and if he choose multiple medicine that also retrieve by using search option.

For the insert routine I have written the following code, but it's not working.
private void btnadd_Click(object sender, EventArgs e)
{
string connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
con.ConnectionString = connectionString;
string SqlString = "insert into Medical_Information ([Reg_No],[Date],[Name],[Doctor_name],[Address],[Quantity],[Medicine_name],[Current_Available],[Manufacture_Name],[Batch_No],[Expiry_Date],[Rupees],[Total]) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
using (cmd = new OleDbCommand(SqlString, con))
{
con.Open();
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Reg_No", lblregno.Text);
cmd.Parameters.AddWithValue("@Date", DateTime.Parse(msktextdate.Text));
cmd.Parameters.AddWithValue("@Name", txtname.Text);
cmd.Parameters.AddWithValue("@Doctor_name", txtdoctorname.Text);
cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
foreach (DataGridViewRow dvgvr in dataGridView1.Rows)
{
int Quantity;
int selectedRowIndex = 6;
bool quanity1 = int.TryParse(dataGridView1.Rows[selectedRowIndex].Cells["Quantity"].Value.ToString(), out Quantity);
cmd.Parameters.AddWithValue("@Quantity", quanity1);
string Medicine_name = dataGridView1.Rows[7].Cells["Medicine_name"].Value.ToString();
cmd.Parameters.AddWithValue("@Medicine_name", Medicine_name);
int Current_Available;
bool Current_Available1 = int.TryParse(dataGridView1.Rows[8].Cells["Current_Available"].Value.ToString(), out Current_Available);
cmd.Parameters.AddWithValue("@Current_Available", Current_Available1);
string Manufacture_Name = dataGridView1.Rows[9].Cells["Manufacture_Name"].Value.ToString();
cmd.Parameters.AddWithValue("@Manufacture_Name", Manufacture_Name);
string Batch_No = dataGridView1.Rows[10].Cells["Batch_No"].Value.ToString();
cmd.Parameters.AddWithValue("@Batch_No", Batch_No);
DateTime datetime;
bool dateTimeHasValue = DateTime.TryParse(dataGridView1.Rows[11].Cells["Expiry_Date"].Value.ToString(), out datetime);
cmd.Parameters.AddWithValue("@Expiry_Date", datetime);
int Rupees;
bool Rupees1 = int.TryParse(dataGridView1.Rows[12].Cells["Rupees"].Value.ToString(), out Rupees);
cmd.Parameters.AddWithValue("@Rupees", Rupees1);
int Total;
bool Total1 = int.TryParse(dataGridView1.Rows[13].Cells["Total"].Value.ToString(), out Total);
cmd.Parameters.AddWithValue("@Total", Total1);
}
int n = cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
 

Answers (2)