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);
Related
I want to do it through sql server in my C# application. I am using the code below. When I search, I get this error.
string keyword = txtDetaylıArama.Text;
SqlDataAdapter sda = new SqlDataAdapter("Select * from tbl_KasaFişi Where Contains (Anahtar, '"+ txtDetaylıArama.Text +"') ", mycon);
DataTable dt = new DataTable();
sda.Fill(dt);
dgvDetaylı.ItemsSource = dt.DefaultView;
Error: 'System.Data.SqlClient.SqlException' türünde bir yakalanamayan özel durum, System.Data.dll öğesinde oluştu
Syntax error near 'Kuş' in the full-text search condition ' Çini Kuş'.
Test following code
string SQL = "Select * from tbl_KasaFişi Where Contains(Anahtar, #DetaylıArama)";
using (SqlConnection conn = new SqlConnection(mycon))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = SQL;
cmd.Parameters.Add("#DetaylıArama", SqlDbType.NVarChar).Value = txtDetaylıArama.Text;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
dgvDetaylı.ItemsSource = dt.DefaultView;
}
}
I am trying to load data from a database into a single DataTable and display it in a DataGridView. When running the code it gives me the exception Incorrect syntax near the keyword 'FROM'
Code
private void button1_Click(object sender, EventArgs e)
{
SqlConnection CON = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\matricsneo\Desktop\New SAD\Villanueva\Villanueva\VillanuevaDB.mdf;Integrated Security=True;User Instance=True");
DataTable DT = new DataTable();
SqlDataAdapter SDA1 = new SqlDataAdapter("SELECT FROM * M101", CON);
SDA1.Fill(DT);
SqlDataAdapter SDA2 = new SqlDataAdapter("SELECT FROM * M102", CON);
SDA2.Fill(DT);
SqlDataAdapter SDA3 = new SqlDataAdapter("SELECT FROM * M103", CON);
SDA3.Fill(DT);
SqlDataAdapter SDA4 = new SqlDataAdapter("SELECT FROM * M104", CON);
SDA4.Fill(DT);
SqlDataAdapter SDA5 = new SqlDataAdapter("SELECT FROM * M105", CON);
SDA5.Fill(DT);
SqlDataAdapter SDA6 = new SqlDataAdapter("SELECT FROM * M106", CON);
SDA6.Fill(DT);
SqlDataAdapter SDA7 = new SqlDataAdapter("SELECT FROM * M107", CON);
SDA7.Fill(DT);
SqlDataAdapter SDA8 = new SqlDataAdapter("SELECT FROM * M108", CON);
SDA8.Fill(DT);
SqlDataAdapter SDA9 = new SqlDataAdapter("SELECT FROM * M109", CON);
SDA9.Fill(DT);
SqlDataAdapter SDA10 = new SqlDataAdapter("SELECT FROM * M110", CON);
SDA10.Fill(DT);
SqlDataAdapter SDA11 = new SqlDataAdapter("SELECT FROM * M111", CON);
SDA11.Fill(DT);
SqlDataAdapter SDA12 = new SqlDataAdapter("SELECT FROM * M112", CON);
SDA12.Fill(DT);
SqlDataAdapter SDA13 = new SqlDataAdapter("SELECT FROM * M113", CON);
SDA13.Fill(DT);
SqlDataAdapter SDA14 = new SqlDataAdapter("SELECT FROM * M114", CON);
SDA14.Fill(DT);
dataGridView1.DataSource = DT;
}
Can anyone please help me with this issue?
check your SELECT strings - those should be SELECT * FROM TABLE instead of SELECT FROM * TABLE
How to convert a C# DateTime variable into Enum of SqlDataType.DateTime?
How to consume that enum into a connection string?
Is doing something like this correct?
string str = "SELECT * FROM TABLE WHERE CreateDt " + <that enum>;
SqlConnection Connection = new SqlConnection (<connection setting>);
Table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(str, Connection);
adapter.FillSchema(Table, SchemaType.Source);
adapter.Fill(Table);
Thank you
Your best option is to use a parameterized command:
var cmd = new SqlCommand();
cmd.Connection = conn;
DateTime MyDate = DateTime.Now;
cmd.CommandText = #"SELECT * FROM TABLE WHERE CreateDt = #MyDate";
cmd.Parameters.AddWithValue("#MyDate", #MyDate);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.FillSchema(Table, SchemaType.Source);
adapter.Fill(Table);
This is not tested, but how about:
string str = "SELECT * FROM TABLE WHERE CreateDt = #createDate";
SqlConnection Connection = new SqlConnection (<connection setting>);
Table mytable = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(str, Connection);
adapter .SelectCommand.Parameters.Add("#createDate", SqlDbType.DateTime)
adapter .SelectCommand.Parameters("#createDate").Value = <Some DateTime>
adapter.FillSchema(mytable, SchemaType.Source);
adapter.Fill(Table);
I have the code below which has multiple DataTables with results and I need to pass a single DataSet with all DataTable values.
MySqlCommand cmd = new MySqlCommand(getLikeInfo, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
MySqlCommand cmd1 = new MySqlCommand(getKnowInfo, con);
MySqlDataAdapter da1 = new MySqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
MySqlCommand cmd2 = new MySqlCommand(getHotelInfo, con);
MySqlDataAdapter da2 = new MySqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MySqlCommand cmd3 = new MySqlCommand(getRoomInfo, con);
MySqlDataAdapter da3 = new MySqlDataAdapter(cmd3);
DataTable dt3 = new DataTable();
da3.Fill(dt3);
MySqlCommand cmd4 = new MySqlCommand(getFoodInfo, con);
MySqlDataAdapter da4 = new MySqlDataAdapter(cmd4);
DataTable dt4 = new DataTable();
da4.Fill(dt4);
How can I do that?
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
...
If you want your tables named in the DataSet you can create your tables from the DataSet
DataSet ds = new DataSet();
DataTable t1 = ds.Tables.Add("t1"); // Create
DataTable t1Again = ds.Tables["t1"]; // fetch by name
You can also set the TableName property of the tables if you use the first approach.
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