TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jahangir Khan
NA
27
5.9k
Changes in my program
May 24 2016 8:21 AM
Now I have 2 combo boxes "Year" & "Amount" on the top of them I do get values for user info, because there are text boxes when called with user ID text boxes fill up with correct data.
The 2 combo boxes are also filled with correct data but I have to manually select year and the amount corresponding to it.
I need help in when I call the data "Year" & "Amount" should appear visable in the combo box and when I lets say select a year then the amount should change accordingly, lat but not the least my reset is not clearing the combo boxes.
Hope you can help, the code is as follows:-
using
System;
using
System.Windows.Forms;
using
System.Data.SqlClient;
using
System.Data;
namespace
dss
{
public
partial
class
Form1 : Form
{
SqlConnection con =
new
SqlConnection(
"Data Source=JAKHAN-PC\\sqlexpress;Initial Catalog=JG_Test;Integrated Security=True"
);
public
Form1()
{
InitializeComponent();
}
private
void
btnSearch_Click(
object
sender, EventArgs e)
{
cmbYear.Items.Clear();
string
sql =
""
;
con.Open();
SqlCommand cmd =
new
SqlCommand();
try
{
sql +=
"SELECT m.MemberId, m.Name, m.Address, m.Cellular, m.Email, p.PaymentId, p.Year, p.Amount from Members as m"
;
sql +=
" INNER JOIN Payments as p ON m.MemberId = p.MemberId"
;
sql +=
" WHERE m.MemberId = '"
+ tbID.Text +
"' ORDER BY p.Year ASC"
;
cmd.Connection = con;
cmd.CommandText = sql;
SqlDataAdapter da =
new
SqlDataAdapter(cmd);
DataTable dt =
new
DataTable();
da.Fill(dt);
con.Close();
if
(dt.Rows.Count >0)
{
for
(
int
i = 0; i<=dt.Rows.Count -1;i++)
{
tbID.Text = dt.Rows[i][
"MemberId"
].ToString();
tbName.Text = dt.Rows[i][
"Name"
].ToString();
tbCellular.Text = dt.Rows[i][
"Cellular"
].ToString();
tbEmail.Text = dt.Rows[i][
"Email"
].ToString();
tbAddress.Text = dt.Rows[i][
"Address"
].ToString();
cmbAmount.Items.Add(dt.Rows[i][
"Amount"
].ToString());
cmbYear.Items.Add(dt.Rows[i][
"Year"
].ToString());
}
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
//This part displaying og the existing data from all the fileds corrssponding within the database//
private
void
btnAdd_Click(
object
sender, EventArgs e)
{
{
con.Open();
string
Sql =
"INSERT INTO Members ( MemberId, Name, Cellular, Email, Address ) VALUES "
+
" (@Id, @name, @cell, @email, @address)"
;
using
(SqlCommand cmd =
new
SqlCommand(Sql, con))
{
cmd.CommandText = Sql;
cmd.Parameters.AddWithValue(
"@Id"
, tbID.Text);
cmd.Parameters.AddWithValue(
"@name"
, tbName.Text);
cmd.Parameters.AddWithValue(
"@cell"
, tbCellular.Text);
cmd.Parameters.AddWithValue(
"@email"
, tbCellular.Text);
cmd.Parameters.AddWithValue(
"@address"
, tbAddress.Text);
cmd.ExecuteNonQuery();
Sql =
"INSERT INTO Payments ( MemberId, [Year], [Amount] ) VALUES "
+
" (@Id, Amount, Year)"
;
cmd.Parameters.Clear();
cmd.CommandText = Sql;
cmd.Parameters.AddWithValue(
"@Id"
, tbID.Text);
cmd.Parameters.AddWithValue(
"@year"
, cmbYear.Text);
cmd.Parameters.AddWithValue(
"@amount"
, cmbAmount.Text);
cmd.ExecuteNonQuery();
MessageBox.Show(
"Data Added"
);
tbID.Clear(); tbName.Clear(); tbCellular.Clear(); tbEmail.Clear(); tbAddress.Clear(); cmbYear.Items.Clear(); cmbAmount.Items.Clear();
con.Close();
}
}
}
//This part represents adding of new input data from all the fileds into the database//
private
void
btnUpdate_Click(
object
sender, EventArgs e)
{
try
{
SqlCommand cmd =
new
SqlCommand();
string
Sql =
"UPDATE Members SET MemberId = '"
+ tbID.Text +
"', Name = '"
+ tbName.Text +
"', Cellular = '"
+ tbCellular.Text +
"', Email = '"
+ tbEmail.Text +
"', Address = '"
+ tbAddress.Text +
"' WHERE MemberId = '"
+ tbID.Text +
"' "
;
cmd.CommandText = Sql;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Sql =
"UPDATE Payments SET MemberId = '"
+ tbID.Text +
"', Year = '"
+ cmbYear.Text +
"', Amount = '"
+ cmbAmount.Text +
"' WHERE MemberId = '"
+ tbID.Text +
"' "
;
cmd.CommandText = Sql;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(
"Data Updated"
);
tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbCellular.Clear(); tbEmail.Clear(); cmbYear.Items.Clear(); cmbAmount.Items.Clear();
}
catch
(Exception error)
{
MessageBox.Show(error.ToString());
}
}
//This part represents deleteing of input data from all the fileds into the database//
private
void
btnDelete_Click(
object
sender, EventArgs e)
{
try
{
SqlCommand cmd =
new
SqlCommand();
string
Sql =
"DELETE FROM Members WHERE MemberId = '"
+ tbID.Text +
"' "
;
cmd.CommandText = Sql;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Sql =
"DELETE FROM Payments WHERE MemberId = '"
+ tbID.Text +
"' "
;
cmd.CommandText = Sql;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbCellular.Clear(); tbEmail.Clear(); cmbYear.Items.Clear(); cmbAmount.Items.Clear();
MessageBox.Show(
"Data Deleted"
);
con.Close();
}
catch
(Exception error)
{
MessageBox.Show(error.ToString());
}
}
//This part represents clearing of input data from all the fileds//
private
void
btnReset_Click(
object
sender, EventArgs e)
{
tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbCellular.Clear(); tbEmail.Clear(); cmbYear.Items.Clear(); cmbAmount.Items.Clear();
}
//This part represents shuting down the application//
private
void
btnExit_Click(
object
sender, EventArgs e)
{
Application.Exit();
}
}
}
Reply
Answers (
0
)
how to edit the details list in grid view ?
How do i show up a value from json