How to query SQL to RichEditControl DevExpress? - sql

I want to fill data from SQL Server to RichEditControl.Text. I used this code but it doesn't work, my RichEditControl.Text has an empty string.
string connectionstring = "Data Source=ADMIN;Initial Catalog=Shoplacviet;Integrated Security=True";
SqlConnection conn = new SqlConnection(connectionstring);
conn.Open();
string query = "select * from so";
SqlCommand cmd = new SqlCommand(query, conn);
var reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
richEditControl1.Options.MailMerge.DataSource = dt;
richEditControl1.Options.MailMerge.ViewMergedData = true;
richEditControl1.Text = dt.ToString();
conn.Close();

Related

How to return Multiple dataset for multiple stored procedures in .net

This is my code:
this.SelectQuery = "stp_customer_master";
this.SelectParameter = new SqlParameter[4];
DataTable dt = new DataTable();
SelectParameter[0] = new SqlParameter();
SelectParameter[0].ParameterName = "#Op4";
SelectParameter[0].Value = "SELECT_TOTAL_CUSTOMER";
SelectParameter[1] = new SqlParameter();
SelectParameter[1].ParameterName = "#Op5";
SelectParameter[1].Value = "SELECT_TOTAL_CUSTOMER_HIGH_PRIORITY";
SelectParameter[2] = new SqlParameter();
SelectParameter[2].ParameterName = "#Op6";
SelectParameter[2].Value = "SELECT_TOTAL_CUSTOMER_MEDIUM_PRIORITY";
SelectParameter[3] = new SqlParameter();
SelectParameter[3].ParameterName = "#Op7";
SelectParameter[3].Value = "SELECT_TOTAL_CUSTOMER_LOW_PRIORITY";
res = this.GetDataSet;
Here I will get result in res = this.GetDataset for all in one result
I want separate results for all 4 stored procedures.
What is the solution?
SqlConnection con=new SqlConnection("Connectionstring");
SqlCommand cmd=new SqlCommand();
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds = new DataSet();
cmd = new SqlCommand("getdata", con); //"getdata"- name of your stored procedure
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
After this you can access the tables in dataset like this
ds.Tables[0]
ds.Tables[1]
in your case you can create common function which accepts the stored procedure name as parameter and returns the dataset for that stored procedure.

Trying to use grid view with my data, but it outputs an error

Following is the code am using to bind the grid, can anyone please suggest me what am doing wrong in this snippet
Dim con As New SqlClient.SqlConnection
Dim ds As New DataSet
Dim adapter As SqlDataAdapter
Dim sql As String
con.ConnectionString = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa#1234"
con.Open()
sql = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=****;Password=*****"
cmd.CommandText = "select * From demo_vb Where ID = '" & txtbox4.Text & "'"
adapter = New SqlClient.SqlDataAdapter
adapter.Fill(ds)
GridView1.ItemsSource = New DataView()
GridView1.DisplayMemberPath = "ID"
con.Close()
I get this error:
The SelectCommand property has not been initialized before calling 'Fill'
try following code:
SqlCommand cmd;
SqlConnection con = new SqlConnection("Data Source=SUNTECH-PC\\SQLEXPRESS;Initial Catalog=iSense;Integrated Security=True;");
con.Open();
cmd = new SqlCommand("Select Picture from Images",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
I think this would help
Dim con As New SqlClient.SqlConnection("Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa#1234")
Dim ds As New DataSet
Dim sql As New SqlCommand("select * From demo_vb Where ID = '" & txtbox4.Text & "'",con)
Dim adapter As New SqlDataAdapter(sql)
con.Open()
adapter.Fill(ds)
GridView1.datasource = ds.Tables(0)
GridView1.databind()
con.Close()

How to use SqlDataSource with button on click to get results from gridview

My overall goal is to be able to type in information into my textbox and click a button to search and get results from the database using gridview. So far I have my SqlDataSource SQL statement set up. I need to figure out how to link my button on click event with my SqlDataSource and Textbox.
Here is what I have so far:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection("Data Source=********;Initial Catalog=*******;User ID=*******;Password=*******");
connection.Open();
}
I am not sure how to use my SqlDataSource that I have already set up.
Updated Code:
SqlConnection sqlCon = new SqlConnection("*******");
SqlDataAdapter dt = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
sqlCon.Open();
dt.SelectCommand = new SqlCommand("SELECT* FROM Core.Form_Section_SubSection_Item_Rel WHERE ([DataCollectionPeriodID] = #DataCollectionPeriodID)", sqlCon);
dt.Fill(ds);
sqlCon.Close();
GridView1.DataBind();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
I will advice you use are stored proc with a parameter and do something like below but you can change this to use your select query, just change commandtype
using (SqlConnection sqlCon = new SqlConnection("connection string"))
{
using (SqlCommand cmd = new SqlCommand())
{
sqlCon.Open();
cmd.Connection = sqlCon;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT* FROM Core.Form_Section_SubSection_Item_Rel WHERE DataCollectionPeriodID = #DataCollectionPeriodID";
cmd.CommandTimeout = 30;
cmd.Parameters.Add("#DataCollectionPeriodID", SqlDbType.VarChar).Value = TextBox1.Text
SqlDataAdapter dt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dt.Fill(ds);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}

How to use the ID of an item after loading combobox from database.?

SqlConnection con = new SqlConnection(#"server=RSTT2; database = Project ; User Id=sa; Password=PeaTeaCee5#");
con.Open();
string strCmd = "select ID,Name from Employee";
SqlCommand cmd = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
da.Fill(ds);
cbSupportID.DataSource = ds;
cbSupportID.DisplayMember = "Name";
cbSupportID.ValueMember = "ID";
cbSupportID.Enabled = true;
cmd.ExecuteNonQuery();
con.Close();
now i want use the id of items in my form to process..Plzz tell me the best solution with code.
now you simply retrieve your selected combobox ID value using
cbSupportID.SelectedValue
eg.
If cbSupportID.SelectedIndex <> -1 Then
textBox1.Text = cbSupportID.SelectedValue.ToString()
End If
You can also access other useful properties like SelectedIndex (starting from 0...) or SelectedItem.
check here: http://msdn.microsoft.com/en-US/library/system.windows.forms.listcontrol.selectedvalue(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

How to get a dataset value

New to VB.Net,
How to insert or select the dataset value.
cmd = New SqlCommand("Select * from table1", con)
ada = New SqlDataAdapter(cmd)
ds = New DataSet
ada.Fill(ds)
cmd = New SqlCommand("Select * from '" & ds.Tables(0) & "' ", con)
mydatatable1.Load(dr3)
It was showing the error in '" & ds.Tables(0) & "', I want to get the dataset value
Need VB.Net Code Help
You have a reasonable idea right up until you get to the point where you are trying to create a second SqlCommand. That is, once you do the Fill, you already have the data in a table. You wouldn't run another select - you've already done that. You'd just reference the table that you want to use in the dataset.
If you want a data table you would do something like this (for VB, see below):
SqlDataAdapter myAdapter = new SqlDataAdapter(CommandText, con);
DataSet myDataset = new DataSet();
myAdapter.Fill(myDataset, "Table"); // "Table" is just a name, it can be anything.
mydatatable1 = myDataset.Tables[0]; // Get the table
Now, if you don't need a DataTable, per se, but just want to read from the query, you'd use a SqlDataReader:
cmd = new SqlCommand(CommandText, con);
// One or more params
cmd.Parameters.AddWithValue("#paramName", Value);
SqlDataReader nwReader = cmd.ExecuteReader();
Then just read from the nwReader:
while (nwReader.Read())
{
string field1Val = (string)nwReader["FieldName"];
etc...
}
Update: I don't know VB but here is what I think it would look like:
cmd = New SqlCommand("Select * from table1", con)
ada = New SqlDataAdapter(cmd)
ds = New DataSet
ada.Fill(ds)
mydatatable1 = ds.Tables(0);
You may well be able to shorten this to get rid of the extra SqlCommand (assuming VB supports this SqlDataAdapater syntax like C#.
ada = New SqlDataAdapter("Select * from table1", con)
ds = New DataSet
ada.Fill(ds)
mydatatable1 = ds.Tables(0);
Good luck...
you can use Microsoft Enterprise Library for making easy this process. this library is a powerful library for working with datasets.
public async Task<ResponseStatusViewModel> GetAll()
{
var responseStatusViewModel = new ResponseStatusViewModel();
var connection = new SqlConnection(EmployeeConfig.EmployeeConnectionString);
var command = new SqlCommand("usp_GetAllEmployee", connection);
command.CommandType = CommandType.StoredProcedure;
try
{
await connection.OpenAsync();
var reader = await command.ExecuteReaderAsync();
var dataSet = new DataSet();
dataSet.Load(reader, LoadOption.OverwriteChanges, new string[] { "Employee" });
reader.Close();
reader.Dispose();
var employees = new List<EmployeeModel>();
if (dataSet.Tables.Count > 0 && dataSet.Tables["Employee"] != null)
{
var employeeTable = dataSet.Tables["Employee"];
for (int i = 0; i < employeeTable.Rows.Count; i++)
{
var employeeRow = employeeTable.Rows[i];
employees.Add(new EmployeeModel
{
EmployeeID = Convert.ToInt32(employeeRow["EmployeeID"]),
EmployeeName = Convert.ToString(employeeRow["EmployeeName"]),
Address = Convert.ToString(employeeRow["Address"]),
GrossSalary = Convert.ToDouble(employeeRow["GrossSalary"]),
PF = Convert.ToDouble(employeeRow["PF"]),
TotalSalary = Convert.ToDouble(employeeRow["TotalSalary"])
});
}
}
responseStatusViewModel.Data = employees;
command.Dispose();
connection.Dispose();
}
catch (Exception ex)
{
throw ex;
}
return responseStatusViewModel;
}
I think what you are looking for is
cmd = New SqlCommand("Select * from '" & ds.Tables(0).TableName & "' ", con)
cmd = New SqlCommand("Select * from table1", con)
ada = New SqlDataAdapter(cmd)
ds = New DataSet
ada.Fill(ds)
cmd = New SqlCommand("Select * from '" & ds.Tables(0).TableName & "' ", con)
mydatatable1.Load(dr3)