Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have one form (Form 1) with my text boxes and a update button and another form (Form 2) with the datagridview. I want to be able to update the form 2 by form 1 update button. Hope someone can help me?

Form 1 code i have so far:

public partial class ChangeReservation : Form
{
SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog =NovaHotelReservationBookingDatabase;Integrated Security=True");
SqlCommand cmd = new SqlCommand();

public SelectReservation SRChild;

public ChangeReservation ()
{
InitializeComponent();

}

public void updateGridReservation()
{

cn.Open();
cmd.CommandText = "update Reservation set Guest_ Name= '" + txtGuestName.Text + "', Children_No='" + comboBoxNoChildren.Text + "', Adults_No='" + comboBoxNoAdults.Text + "', CheckinDate='" + dateCheckin.Text + "',CheckoutDate='" + dateCheckOut.Text + "', NumberOfDays='" + txtDaysNumber.Text + "', Room_No= '" + txtRoomNumber.Text + "', RoomType= '" + txtRoomType.Text + "',RoomRate= '" + txtRoomRate.Text + "', Total= '" + txtTotal.Text + "' where Guest_Name='" + SRChild.gridReservation.SelectedRows[0].Cells[4].Value.ToString() + "'";
cmd.ExecuteNonQuery();

cn.Close();
// loadgridGuest();
// cleartxt();
MessageBox.Show("Successfully Updated");
}

private void btnUpdate_Click_1(object sender, EventArgs e)
{

updateGridReservation();

}

Form 2 code:

public partial class SelectReservation : Form
{

SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog =NovaHotelReservationBookingDatabase;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;

private string ReserveID;
private string GName;
private string NoOfChildren;
private string NoOfAdults;
private string DateCheckin;
private string DateCheckout;
private string NoOfdays;
private string RoomNumber;
private string TypeOfRoom;
private string RateOfRoom;
private string TotalCost;
private string RmID;
private string GuestID;

public ChangeReservation CRParent { get; set; }

public SelectReservation()
{
InitializeComponent();

}

private void SelectReservation_Load(object sender, EventArgs e)
{
cmd.Connection = cn;

loadgridReservation();
}

void loadgridReservation()
{
cn.Open();
cmd.CommandText = "select * from Reservation";
dr = cmd.ExecuteReader();
gridReservation.Rows.Clear();

if (dr.HasRows)
{
while (dr.Read())
{
int n = gridReservation.Rows.Add();
gridReservation.Rows[n].Cells[0].Value = dr[0].ToString();
gridReservation.Rows[n].Cells[1].Value = dr[1].ToString();
gridReservation.Rows[n].Cells[2].Value = dr[2].ToString();
gridReservation.Rows[n].Cells[3].Value = dr[3].ToString();
gridReservation.Rows[n].Cells[4].Value = dr[4].ToString();
gridReservation.Rows[n].Cells[5].Value = dr[5].ToString();
gridReservation.Rows[n].Cells[6].Value = dr[6].ToString();
gridReservation.Rows[n].Cells[7].Value = dr[7].ToString();
gridReservation.Rows[n].Cells[8].Value = dr[8].ToString();
gridReservation.Rows[n].Cells[9].Value = dr[9].ToString();
gridReservation.Rows[n].Cells[10].Value = dr[10].ToString();
gridReservation.Rows[n].Cells[11].Value = dr[11].ToString();
gridReservation.Rows[n].Cells[12].Value = dr[12].ToString();

}
}
cn.Close();
}

private void gridReservation_CellClick(object sender, DataGridViewCellEventArgs e)
{
ReserveID = gridReservation.SelectedRows[0].Cells[0].Value.ToString();
RmID = gridReservation.SelectedRows[0].Cells[1].Value.ToString();
GuestID = gridReservation.SelectedRows[0].Cells[2].Value.ToString();
GName = gridReservation.SelectedRows[0].Cells[3].Value.ToString();
NoOfChildren = gridReservation.SelectedRows[0].Cells[4].Value.ToString();
NoOfAdults = gridReservation.SelectedRows[0].Cells[5].Value.ToString();
DateCheckin = gridReservation.SelectedRows[0].Cells[6].Value.ToString();
DateCheckout = gridReservation.SelectedRows[0].Cells[7].Value.ToString();
NoOfdays = gridReservation.SelectedRows[0].Cells[8].Value.ToString();
RoomNumber = gridReservation.SelectedRows[0].Cells[9].Value.ToString();
TypeOfRoom = gridReservation.SelectedRows[0].Cells[10].Value.ToString();
RateOfRoom = gridReservation.SelectedRows[0].Cells[11].Value.ToString();
TotalCost = gridReservation.SelectedRows[0].Cells[12].Value.ToString();
}

private void btnSubmit_Click(object sender, EventArgs e)
{
CRParent.GetAndDisplayReservationDetails(ReserveID, RmID, GuestID, GName, NoOfChildren, NoOfAdults, DateCheckin, DateCheckout, NoOfdays, RoomNumber, TypeOfRoom, RateOfRoom, TotalCost);
this.Close();

}

private void btnSearchGuest_Click(object sender, EventArgs e)
{

searchGuestName();
}

void searchGuestName()
{
cn.Open();
cmd.CommandText = "select * from Reservation where Guest_Name = '" + txtGuestName.Text + "';";
dr = cmd.ExecuteReader();
gridReservation.Rows.Clear();

if (dr.HasRows)
{
while (dr.Read())
{
int n = gridReservation.Rows.Add();
gridReservation.Rows[n].Cells[0].Value = dr[0].ToString();
gridReservation.Rows[n].Cells[1].Value = dr[1].ToString();
gridReservation.Rows[n].Cells[2].Value = dr[2].ToString();
gridReservation.Rows[n].Cells[3].Value = dr[3].ToString();
gridReservation.Rows[n].Cells[4].Value = dr[4].ToString();
gridReservation.Rows[n].Cells[5].Value = dr[5].ToString();
gridReservation.Rows[n].Cells[6].Value = dr[6].ToString();
gridReservation.Rows[n].Cells[7].Value = dr[7].ToString();
gridReservation.Rows[n].Cells[8].Value = dr[8].ToString();
gridReservation.Rows[n].Cells[9].Value = dr[9].ToString();
gridReservation.Rows[n].Cells[10].Value = dr[10].ToString();
gridReservation.Rows[n].Cells[11].Value = dr[11].ToString();
gridReservation.Rows[n].Cells[12].Value = dr[12].ToString();


}
}
cn.Close();
}
Posted

1 solution

For starters, stop doing that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Secondly, how you do this depends on the "relationship" between the forms:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

And thirdly your code won't work: You need to assign an instance of your form to SRChild and call either Show or ShowDialog to display the second form. If you don't create the instance, you will just get a null reference exception.



"can you give me example then on how to do properly as i am new to c# please? and i don't want the second form to show again once i updated datagridview from form 1"

Try:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable WHERE UserName=@NM", con))
        {
        cmd.Parameters.AddWithValue("@NM", tbUserName.Text);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["Id"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", id, desc);
                }
            }
        }
    }

For an example of how to do multiform stuff, have a look at the links - they demonstrate the communications and have an example app.
 
Share this answer
 
v2
Comments
Member 11747266 7-Jun-15 3:17am    
can you give me example then on how to do properly as i am new to c# please? and i don't want the second form to show again once i updated datagridview from form 1
OriginalGriff 7-Jun-15 3:29am    
Answer updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900