Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Inserting data from two forms into SQL C#

Inserting data from two forms into SQL C#

Inserting data from two forms into SQL C#

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[i][0].ToString());
ad.SASIA = int.Parse(dataTable.Rows[i][2].ToString());
ad.CMIMI = int.Parse(dataTable.Rows[i][3].ToString());
ad.TVSH = int.Parse(dataTable.Rows[i][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[i][7].ToString());
ad.VLERAPATVSH = float.Parse(dataTable.Rows[i][6].ToString());
ad.NRATIKUJVE = int.Parse(lblnumri.Text);
ad.TOTALIPCS = float.Parse(dataTable.Rows[i][5].ToString());
//and finally



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;


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));

If someone could help me about where i am missing something, that don't make the code work.
Thanks to everyone!

RE: Inserting data from two forms into SQL C#

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)
ad = _ad;

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close