Click here to Skip to main content
15,114,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
"SELECT course.Courseno, subject.PaperName
                                 FROM course INNER JOIN
                                    ids ON course.Courseno = ids.Courseno INNER JOIN
                                            subject ON ids.PaperID = subject.PaperID"


it has to be specified as command.commandtext=".......
I am using windows forms
Posted
Comments
Prabhakaran Soundarapandian 24-Aug-12 5:47am
   
you want to get the query form textbox?
codingisok101 24-Aug-12 6:00am
   
from a list item

1 solution

Try this
SQL
SqlCommand command = new SqlCommand();
    command.CommandText = @"SELECT course.Courseno, subject.PaperName
                           FROM course INNER JOIN
                           ids ON course.Courseno = ids.Courseno INNER JOIN
                           subject ON ids.PaperID = subject.PaperID";

    command.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
Dataset ds = new Dataset();
da.fill(ds) ;
GridViewID.DataSource = ds.Tables[0];
   
v2
Comments
codingisok101 24-Aug-12 6:13am
   
i want to display this output on a gridview.. is it possible?
pradiprenushe 24-Aug-12 6:23am
   
See my updated solution
codingisok101 24-Aug-12 6:29am
   
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string currentitem = listBox1.SelectedItem.ToString();

command.Parameters.Clear();
command.Parameters.AddWithValue("@Courseno", listBox1.Items);
command.CommandText = @"SELECT course.Courseno, subject.PaperName
FROM course INNER JOIN
ids ON course.Courseno = ids.Courseno INNER JOIN
subject ON ids.PaperID = subject.PaperID";
command.Parameters.Clear();

dataset.Tables.Clear();

adapter.Fill(dataset);

dataGridView1.DataSource = dataset;
}

I am missing something here. I am unable to see anything in my datagridview
codingisok101 24-Aug-12 6:30am
   
this is my entire code.


namespace final2
{
public partial class Form1 : Form
{
private SqlConnection con;
private SqlCommand command;
private SqlDataAdapter adapter;
private DataSet dataset;

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

command.Parameters.Clear();
command.Parameters.AddWithValue("@Courseno", listBox1.Items);
command.CommandText = @"SELECT course.Courseno, subject.PaperName
FROM course INNER JOIN
ids ON course.Courseno = ids.Courseno INNER JOIN
subject ON ids.PaperID = subject.PaperID";
command.Parameters.Clear();

dataset.Tables.Clear();

adapter.Fill(dataset);

dataGridView1.DataSource = dataset;
}

}
}
pradiprenushe 24-Aug-12 6:33am
   
Remove dataset.Tables.Clear();
Add breakpoint at dataGridView1.DataSource = dataset;
Right click dataset & quick watch. See whether data is coming.
codingisok101 24-Aug-12 6:42am
   
see my problem is i cant select more than table in the datagrid source option. and i need to output the result of the sql query onto a datagrid. how do i do this?
pradiprenushe 24-Aug-12 6:45am
   
But for that you must check whether your query is returing result.
Check this what message it gives
adapter.Fill(dataset);
Messagebox.Show(dataset.Tables[0].Rows.Count.ToString());
dataGridView1.DataSource = dataset;

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