datagrid doesn't bind with datatable - c#

I'm working on a webpage in azure. Here's a part of my code:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
_adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Off course that instead of "my connection string" there's the real one...
The problem is that the page loads but without the gridview for some reason...
Any help will be great, Thanks
EDIT: I also tried with datasets, like this:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
_adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

Try with GridView1.DataSource = ds.Tables[0];
whole code will look as follows:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataSet ds = new DataTable();
_adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

Related

Fill combobox from SQL database with specific parameter

I have problem to get specific value from sql server with parameter can anybody explain me why it works on winfom but not on wpf and how i can fix it
my code:
private void UpdateItems()
{
COMBOBOX1.Items.Clear();
SqlConnection conn = new SqlConnection(Properties.Settings.Default.constring.ToString());
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM CLIENT where cod_cli='some_specific_string'", conn);
DataSet ds = new DataSet();
da.Fill(ds, "CLIENT");
COMBOBOX1.ItemsSource = ds.Tables[0].DefaultView;
COMBOBOX1.DisplayMemberPath = ds.Tables[0].Columns["FR"].ToString();
COMBOBOX1.SelectedValuePath = ds.Tables[0].Columns["FC"].ToString();
}
The program when execute this function crash with error:
System.Data.SqlClient.SqlException: 'Invalid column name
'some_specific_string'.'
the solution is
SqlConnection sqlConnection = new SqlConnection(Properties.Settings.Default.constring.ToString());
{
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM CLIENTS where cod_cli=#cod", sqlConnection);
sqlCmd.Parameters.AddWithValue("#cod", cod_cli.Text);
sqlConnection.Open();
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
while (sqlReader.Read())
{
COMBOBOX1.Items.Add(sqlReader["FR"].ToString());
}
sqlReader.Close();
}
The query doesn't recognize string as parameter but adding as SQL parameter it works.
SqlConnection conn = new SqlConnection(Properties.Settings.Default.constring.ToString());
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM CLIENT where cod_cli='some_specific_string'", conn);
DataSet ds = new DataSet();
da.Fill(ds, "CLIENT");
//Populate the combobox
COMBOBOX1.ItemsSource = ds.Tables[0].DefaultView;
COMBOBOX1.DisplayMemberPath = "FR";
COMBOBOX1.SelectedValuePath = "FC";
where "FR" and "FC" are existing columns in your SELECT Query.

the selectcommand property has not been initialized before calling fill c# in wpf

I have written following code in wpf but when clicked on button it shows an error.
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
SqlConnection con = new SqlConnection("data source=RAMANDEEP-PC\\SQLEXPRESS; initial catalog=RamandeepSingh; integrated security=true");
con.Open();
SqlCommand cmd = new SqlCommand("select * from ContactManager", con);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.Fill(ds, "ContactManager");
da.SelectCommand = cmd;
listBox1.DataContext = ds.Tables[0];
con.Close();
}
catch (Exception we)
{
MessageBox.Show(we.Message);
}
}
The error is pretty straight forward .you are trying to fill the adapter first and then calling the select command. Which is obviously wrong. Change it to
da.SelectCommand = cmd;
da.Fill(ds, "ContactManager");
Sequence matters, try this. :)
DataSet ds = new DataSet();
using(SqlConnection conn = new SqlConnection(""data source=RAMANDEEP-PC\\SQLEXPRESS; initial catalog=RamandeepSingh; integrated security=true"))
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from ContactManager";
da.SelectCommand = cmd;
conn.Open();
da.Fill(ds);
}
listBox1.ItemSource = ds.Tables[0];
The error message clearly states the issue in your code. You should set SelectCommand before calling Fill which internally uses it.
da.SelectCommand = cmd;
da.Fill(ds, "ContactManager");

Search Command without using WHERE CLAUSE while connected to sqldatasource

the problem is i can use my database if my gridview is not connected to database using sqldatasource
it will work if my database is connected manually using code to gridview
i need a code to search my data using datagrid and textbox and button with the search code in it
here's my current code for search command
but its only working when i connect my datagrid to database manually using code
strconn = "select * from User_TBL_DB where (Lastname like '%' + #search +'%')";
SqlCommand xp = new SqlCommand(strconn, conn);
xp.Parameters.Add("#search", SqlDbType.NVarChar).Value = SearchBOX.Text;
conn.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
GridView2.DataSource = ds;
GridView2.DataBind();
conn.Close();
hope you can help me
SQL Command is not correct. It should be ... LIKE '%#Search%'
var ds = new DataSet();
using (var cnn = new SqlConnection(CONNECTION_STRING))
{
string cmdText = "SELECT * FROM User_TBL_DB WHERE Lastname LIKE '%#Search%'";
var cmd = new SqlCommand(cmdText, cnn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#Search", SearchBOX.Text);
cnn.Open();
var da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "Name");
}
GridView2.DataSource = ds;
GridView2.DataBind();

DataGridView binding not working

I am working on a winforms project and i have this following code in the Form_Load method. But it doesnt work. Can anyone help me?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sella.Properties.Settings.Database1ConnectionString1"].ConnectionString);
// A SqlCommand object is used to execute the SQL commands.
SqlCommand scmd = new SqlCommand("Select * From CustCalls", conn);
// A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
SqlDataAdapter sda = new SqlDataAdapter(scmd);
// Create and Fill a new DataSet.
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds;
Try sourcing directly to the table in the dataset:
dataGridView1.DataSource = ds.Tables[0];
SqlDataAdapter sda = new SqlDataAdapter(scmd, conn);
dataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;

Problem with SqlConnection, ASP.net C#

I have the following C# code:
public string TargetDate()
{
SqlConnection con =
new SqlConnection("Server=localhost;Database=Timer;Trusted_Connectopn=True");
SqlCommand cmd = new SqlCommand("select * from Timer");
con.Open();
DataSet ds = new DataSet(cmd,con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();
}
but I get error at the: new DataSet(cmd,con); ...
the error: CS1502: The best overloaded
method match for
'System.Data.DataSet.DataSet(System.Runtime.Serialization.SerializationInfo,
System.Runtime.Serialization.StreamingContext)'
has some invalid arguments
What is could be the problem?
Try this:
SqlConnection con = new SqlConnection
("Server=localhost;Database=Timer;Trusted_Connection=True");
SqlCommand cmd = new SqlCommand("select * from Timer", con);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
This is even better:
DataTable dataTable = new DataTable();
using(SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))
using(SqlCommand command = connection.CreateCommand())
{
command.CommandText = "select * from Timer";
connection.Open();
SqlDataReader reader = command.ExecuteReader();
dataTable.Load(reader);
}
You've got the wrong constructor for the DataSet. Try this
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(con);
It seems you have mixed up the constructors:
Try the following:
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(con);
Hope that helps

Categories