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
Related
Getting Error in Csharp :
string sq = "select EmpName,contactnumber,dob,Address from tbl_emp_details where empname = #empname";
SqlCommand cmd = new SqlCommand(sq, con);
cmd.Parameters.AddWithValue("#empname", comboBox1.SelectedText);
SqlDataAdapter adp = new SqlDataAdapter(sq, con);
// com.Parameters.AddWithValue("#empname",Name.SelectedText);
DataTable dt = new DataTable();
adp.Fill(dt);
Name.Text = dt.Rows[0]["Empname"].ToString();
You're creating the data adapter using the original SQL and not the command (which includes the parameter) that you create.
Change
SqlDataAdapter adp = new SqlDataAdapter(sq, con);
to
SqlDataAdapter adp = new SqlDataAdapter(cmd);
How can I fill a GridView in foreach because this code bring last row just not all.
foreach (int i in userdetails)
{
SqlConnection con = new SqlConnection("*****");
SqlCommand cmd = new SqlCommand("********=" + i, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvSelected.Visible = true;
gvSelected.DataSource = ds;
gvSelected.DataBind();
}
You will want to do something more like this. Only get the data in the loop.
Notice I also used a SqlParameter which is important to prevent SQL Injection and avoid syntax errors.
SqlConnection con = new SqlConnection("*****");
SqlCommand cmd = new SqlCommand("********=#id", con);
SqlParameter parId = new SqlParameter("#id", SqlDbType.Int);
cmd.Parameters.Add(parId);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
foreach (int i in userdetails)
{
parId.Value = i;
da.Fill(ds);
}
gvSelected.Visible = true;
gvSelected.DataSource = ds;
gvSelected.DataBind();
Of course you should consider trying to send all ids at once if possible. You would need something like a Table Valued parameter for that.
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.
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");
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();
}