Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting data from two forms into SQL C#

Status
Not open for further replies.

leomi

Programmer
Jun 13, 2020
1
AL
Hi everyone.
I am trying to insert data from two forms into SQL. The form 1 contains some textbox and a datagridview , and Form2 contains two textboxes. When the user click button"btnform2", the form two is opened but the form 1 still remains opened. Now i want when user click the button save(form2) , the data to be saved into sql from two forms.
I created a class
public class arka_data
{
public int NR { get; set; }
public int BARKODI { get; set; }
public string EMERTIMI { get; set; }
public int SASIA { get; set; }
public float CMIMI { get; set; }
public float TVSH { get; set; }
public float TOTAL { get; set; }
public float NENTOTALI { get; set; }
public float ZBRITJA { get; set; }
public float TOTALI { get; set; }
public DateTime KOHA { get; set; }
public string KASIERI { get; set; }
public string KLIENTI { get; set; }
public float VLERAETVSH { get; set; }
public float VLERAPATVSH { get; set; }
public int NRATIKUJVE { get; set; }
public float TOTALIPCS { get; set; }
public float VLERATVSHTOTAL { get; set; }


}

And a method using the upper class to collect data

public void mbushe(string[] args)
{
for (int i = 0; i < dataTable.Rows.Count; i++)
{
arka_data ad = new arka_data();
ad.NR = int.Parse(txtnrfatures.Text);
ad.VLERATVSHTOTAL = float.Parse(textBox1.Text);
ad.BARKODI = int.Parse(dataTable.Rows[0].ToString());
ad.SASIA = int.Parse(dataTable.Rows[2].ToString());
ad.CMIMI = int.Parse(dataTable.Rows[3].ToString());
ad.TVSH = int.Parse(dataTable.Rows[4].ToString());
ad.NENTOTALI = float.Parse(txttotali.Text);
ad.ZBRITJA = float.Parse(txtzbritja.Text);
ad.TOTALI = float.Parse(totali.Text);
ad.KOHA = DateTime.Now;
ad.KASIERI = lbluser.Text;
ad.KLIENTI = cmbklienti.Text;
ad.VLERAETVSH = float.Parse(dataTable.Rows[7].ToString());
ad.VLERAPATVSH = float.Parse(dataTable.Rows[6].ToString());
ad.NRATIKUJVE = int.Parse(lblnumri.Text);
ad.TOTALIPCS = float.Parse(dataTable.Rows[5].ToString());
//and finally
Program.dta.Add(ad);


}

}

But in form 2 , there are not passing data from form 1 .
The code i am using to store data

private void button1_Click(object sender, EventArgs e)
{

arka_data ad = new arka_data();
string faturimi = "Metoda e pagesese" + " "+ "KESH;" +"Paguar"+ txtpaguar.Text + " "+ "Kusuri"+ textBox3.Text;

con.Open();

SqlCommand cmd = new SqlCommand("insertfaturimi", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@nrfatures", ad.NR));
cmd.Parameters.Add(new SqlParameter("@klienti", ad.KLIENTI));
cmd.Parameters.Add(new SqlParameter("@pagesa", faturimi));
cmd.Parameters.Add(new SqlParameter("@nentotali", ad.NENTOTALI));
cmd.Parameters.Add(new SqlParameter("@zbritje", ad.ZBRITJA));
cmd.Parameters.Add(new SqlParameter("@totali", ad.TOTALI));
cmd.Parameters.Add(new SqlParameter("@vleratvsh", ad.VLERATVSHTOTAL));
cmd.Parameters.Add(new SqlParameter("@nrartikujve", ad.NRATIKUJVE));
cmd.Parameters.Add(new SqlParameter("@kasieri", ad.KASIERI));
cmd.Parameters.Add(new SqlParameter("@koha", DateTime.Now));
cmd.Parameters.Add(new SqlParameter("@barkodi", ad.BARKODI));
cmd.Parameters.Add(new SqlParameter("@emertimi", ad.EMERTIMI));
cmd.Parameters.Add(new SqlParameter("@sasia", ad.SASIA));
cmd.Parameters.Add(new SqlParameter("@tvsh", ad.TVSH));
cmd.Parameters.Add(new SqlParameter("@cmimi", ad.CMIMI));
cmd.Parameters.Add(new SqlParameter("@totalipcs", ad.TOTALIPCS));
cmd.Parameters.Add(new SqlParameter("@vlerapatvshpcs", ad.VLERAPATVSH));
cmd.Parameters.Add(new SqlParameter("@vleraetvshpcs", ad.VLERAETVSH));
cmd.ExecuteNonQuery();
}


If someone could help me about where i am missing something, that don't make the code work.
Thanks to everyone!
 
you are setting the data on your form one
but you have to pass that object to form 2 to use it.


arka_data ad;
public Form2(arka_data _ad)
{
InitializeComponent();
ad = _ad;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top