suppose i link one form to another, and i want to write an sql query to output the result onto the second form, using a datagrid view, how do i go about doing this?
my code so far is..
Form2 secondform=new Form2();
private void button3_Click(object sender, EventArgs e)
{
secondform.Show();
}
how do i go about writing the code now?
Addition, full code from comment:
namespace final2
{
public partial class Form1 : Form
{
private SqlConnection con;
private SqlCommand command;
private SqlDataAdapter adapter;
private DataSet dataset;
public DataGridView dg;
public Form1()
{
InitializeComponent();
con = new SqlConnection();
command = con.CreateCommand();
con.ConnectionString = "Data Source=CASSINI-003-PC;Initial Catalog=studentdb;Integrated Security=True";
adapter = new SqlDataAdapter(command);
dataset = new DataSet();
}
private void button1_Click(object sender, EventArgs e)
{
command.Parameters.Clear();
command.Parameters.AddWithValue("@StudentID", textBox1.Text);
command.Parameters.AddWithValue("@Name", textBox2.Text);
command.Parameters.AddWithValue("@Age", textBox3.Text);
command.Parameters.AddWithValue("@Gender", textBox4.Text);
command.Parameters.AddWithValue("@Courseno", listBox1.SelectedItem);
command.CommandText = "INSERT into details" + "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";
try
{
con.Open();
int result = command.ExecuteNonQuery();
if (result > 0)
MessageBox.Show("student successfully updated");
else
MessageBox.Show("failed to update");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
ClearFields();
}
private void button2_Click(object sender, EventArgs e)
{
command.Parameters.Clear();
command.Parameters.AddWithValue("@StudentID", textBox1.Text);
command.CommandText = "SELECT * FROM details WHERE StudentID=@StudentID";
dataset.Tables.Clear();
int result = adapter.Fill(dataset, "details");
if (result > 0)
{
DataRow srow = dataset.Tables["details"].Rows[0];
textBox1.Text = srow["StudentID"].ToString();
textBox2.Text = srow["Name"].ToString();
textBox3.Text = srow["Age"].ToString();
textBox4.Text = srow["Gender"].ToString();
listBox1.SelectedItem = srow["Courseno"].ToString();
}
else
{
MessageBox.Show("Student does not exist");
}
}
void ClearFields()
{
textBox1.Text = String.Empty;
textBox2.Text = String.Empty;
textBox3.Text = String.Empty;
textBox4.Text = String.Empty;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string currentitem = listBox1.SelectedItem.ToString();
}
Form2 secondform=new Form2();
private void button3_Click(object sender, EventArgs e)
{
secondform.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
this.resultTableAdapter.Fill(this.studentdbDataSet1.result);
}
}
}