1
Answer

How to display multiple values to a column in database?

 I have a different subject table and want to access multiple subjects name to subname attribute of exam_tbl through inserting student data . I want to insert data to tbl_exam on inserting to student table. 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace SISv2._0
{
public partial class AddStudent : Form
{
String regno = DBHandler.getGenerateNo();
public AddStudent()
{
InitializeComponent();
}
private void AddStudent_Load(object sender, EventArgs e)
{
loadData();
}
private void btn_Save_Click(object sender, EventArgs e)
{
InsertData();
}
private void btn_Reset_Click(object sender, EventArgs e)
{
ClearData();
}
private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btn_Browse_Click(object sender, EventArgs e)
{
PicBrowse();
}
private void InsertData()
{
if (txt_class.Text != "")
{
try
{
//------Image Making
Byte[] imageByte;
MemoryStream ms = new MemoryStream();
imageBox.Image.Save(ms, ImageFormat.Png);
imageByte = new Byte[ms.Length];
ms.Read(imageByte, 0, imageByte.Length);
ms.Close();
//------------------------->
DBHandler.OpenConnection();
string qry = "Insert into Student values ( '','" + regno + "','" + txt_sname.Text.Trim().ToString() + "','" + txt_class.Text.Trim().ToString() + "','" + txt_section.Text.Trim().ToString() + "','" + txt_roll.Text.Trim().ToString() + "','" + txt_fiscal.Text.Trim().ToString() + "','" + txt_saddress.Text.Trim().ToString() + "','" + txt_sdob.Text.Trim().ToString() + "','" + txt_pname.Text.Trim().ToString() + "','" + txt_occupation.Text.Trim().ToString() + "','" + txt_contact.Text.Trim().ToString() + "',@img)";
List<MySqlParameter> sqlParameter = new List<MySqlParameter>();
//imageparameter
sqlParameter.Add(new MySqlParameter() { MySqlDbType = MySqlDbType.Blob, ParameterName = "img", Value = imageByte });
//--------------Adding the parameter list
DBHandler.ExecuteQryNonQry(qry, sqlParameter);//Execute the non query Parameter
MessageBox.Show("Data Saved");
// DBHandler.CloseConnection();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
DBHandler.CloseConnection();
ClearData();
loadData();
}
}
else
{
MessageBox.Show("Please insert full data correctly!!!");
}
// switchfunction(regno,txt_class.Text.ToString());
}
private void ClearData()
{
txt_sname.Clear();
txt_sid.Clear();
txt_saddress.Clear();
txt_class.SelectedIndex = -1;
txt_section.SelectedIndex = -1;
txt_roll.ResetText();
txt_pname.Clear();
txt_fiscal.SelectedIndex = -1;
txt_occupation.Clear();
txt_contact.Clear();
}
private void PicBrowse()
{
OpenFileDialog OF = new OpenFileDialog();
OF.Multiselect = false;
OF.Filter = "Image File |*.jpg;*.png;*.jpeg";
if (OF.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
imageBox.Image = Image.FromFile(OF.FileName);
}
}
private void loadData()
{
try
{
dataGridView1.Rows.Clear();
DBHandler.OpenConnection();
DBHandler.ExecuteQry("Select * from Student order by STDID asc", false);
MySqlDataReader sdr = DBHandler.sqlDataReader;
while (sdr.Read())
{
dataGridView1.Rows.Add(false, sdr[0], sdr[1], sdr[2], sdr[3], sdr[4], sdr[5], sdr[6], sdr[7], sdr[8], sdr[9], sdr[10], sdr[11]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
DBHandler.CloseConnection();
}
}
public void switchfunction()
{
try
{
// string test3 = test2;
int caseSwitch = Int32.Parse(txt_class.Text.ToString());
string s;
String current = DateTime.Now.ToString("dd-MM-yyyy");
switch (caseSwitch)
{
case 1:
{
String sclass = "1";
MySqlDataReader sdr = DBHandler.FetchData("Select * from subject where SUBCLASS ='" + sclass + "'");
while (sdr.Read())
{
s = (string)sdr["SUBNAME"];
MessageBox.Show(""+s);
catch (MySqlException ex) {
MessageBox.Show(ex.Message);
}
}
}
}
Answers (1)