SQL Server 2008 dbms and Visual Studio - sql

I need to add one more column in gridview as difference of Supplier_Quantity - Store_quantity and that difference should be stored in new column after the supplier_Quantity.
But, when I click on Calculate button column what should I do then ?
I tried the following query:
select
Product_Name, Supplier_Quantity, Store_Quantity,
'DIFFRENCE' = Supplier_Quantity - Store_Quantity
from
relatesupp
but it shows in sql only and as soon as I use it in Visual Studio it doesn't show in gridview.

You can place the code in the button and get the desired result in the datagridview :
string sQuery = "select Product_Name, Supplier_Quantity, Store_Quantity, Supplier_Quantity - Store_Quantity As 'DIFFRENCE' "
+"from relatesupp";
SqlCommand cmd = new SqlCommand(sQuery, con);
SqlDataReader sdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(sdr);
dataGridView1 .DataSource = dt;

Related

How to get DevExpress GridControl to show captions instead of field names?

I have a DevExpress GridControl with a master and and detail. On the detail, I want to show one column. The dataset I'm using has two columns, the one I want to show and the foreign key back to the master. In the designer, I've included only the column I want and in design mode it show one column. However, when I run the application I see both columns with the names from the data set. How do I get the detail grid to just show the one column I want?
Here's an example of my code. I've tried it with and without the level tree add and tried different names for the relation. Nothing I've tried uses the formatting I set in the designer for the detail view.
Dim cmd As SqlCommand
Dim da As SqlDataAdapter
Dim da2 As SqlDataAdapter
cmd = MSSQL_CONN.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from vw_backlog with (nolock) where order_id = " & m_order.id
da = New SqlDataAdapter(cmd)
cmd = MSSQL_CONN.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT order_id, backlog_id, special_handling_id, comment_id, message FROM vw_backlog_special_handling_comments with (nolock) WHERE order_id = " & m_order.id
da2 = New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "Backlogs")
da2.Fill(ds, "Messages")
Dim keyColumn As DataColumn = ds.Tables("Backlogs").Columns("backlog_id")
Dim foreignKeyColumn As DataColumn = ds.Tables("Messages").Columns("backlog_id")
ds.Relations.Add("gvSpecialHandlingComments", keyColumn, foreignKeyColumn)
gcBacklog.DataSource = ds.Tables("Backlogs")
gcBacklog.ForceInitialize()
gcBacklog.LevelTree.Nodes.Add("SpecialHandlingComments", gvSpecialHandlingComments)
gvSpecialHandlingComments.PopulateColumns(ds.Tables("Messages"))

How to add data for sql to combobox?

this code is working
TextBoxService is a combobox
Connection()
Dim sql As String
CON.Open()
DTS = New DataSet
sql = "SELECT * FROM Service"
dap = New SqlDataAdapter(sql, CON)
dap.Fill(DTS, "Service")
TextBoxService.DataSource = DTS.Tables("Service")
TextBoxService.DisplayMember = "ServiceName"
TextBoxService.ValueMember = "ServiceID"
because I need to at it to 2 comboboxes. it will change both comboboxes when I selectd a combobox.
so i try to use this
bds = New BindingSource
DTS = New DataSet
bds.DataSource = DTS.Tables("Service")
TextBoxService.DataSource = bds
TextBoxService.DisplayMember = "ServiceName"
TextBoxService.ValueMember = "ServiceID"
but it show this error Cannot bind to the new display member
There is more than one way to this the easy and simple I find is if you
*import the SQL data base in data sources
*Select the table you want and set the object type eg( combobox, textbox or checkbox)
*Then select eg combobox and click on the small arrow to the top left hand corner
*Select you data source(table), display member(field name)
* then go to the properties of the combobox object and select data binding plus sign
* click on text and click the data source (table) and field you want to display the data from
You can also do it as
Declare SQLConnection Declare SQLDataReader Declare SQLCommand
Try
If Con.State = ConnectionState.Closed Then
Con.Open()
cmd.Connection = Con
cmd.CommandText = "Select field1, field2 from table"
dr = cmd.ExecuteReader()
' Fill a combo box with the datareader
Do While dr.Read = True
ComboBoxName.Items.Add(dr.GetString(0))
ComboBoxName.Items.Add(dr.GetString(1))
Loop
Con.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Hope it works for you.

Linking two DataGridViews

I have two Grid controls in VB.Net Winform application connected to Oracle Database. The first one shows a table 1 with fields (among others) ID and TaskName. The second one shows a linked table with fields (among others) Task. In the logic of the application the two tables are linked (Table 1, Field ID) <----> (Table 2, Field Task).
I would like, when selecting a row in the first DataGridView, have only the linked row in the second GridView but I can't make it work.
Here is my code attempt :
Dim SQLQuery As String = "SELECT * FROM SCHEME.ARCH_TASKS"
Conn = New OracleConnection(ConnectionString)
Command = New OracleCommand(SQLQuery, Conn)
DataAdapter = New OracleDataAdapter(Command)
DataSet = New DataSet()
DataTable1 = New DataTable
DataSet.Tables.Add(DataTable1)
DataAdapter.Fill(DataTable1)
DataGridView.DataSource = DataTable1.DefaultView
GridControl1.DataSource = DataTable1.DefaultView
SQLQuery = "SELECT * FROM SCHEME.ARCH_LINK_ROLE_TASK"
Command = New OracleCommand(SQLQuery, Conn)
DataAdapter = New OracleDataAdapter(Command)
DataTable2 = New DataTable
DataSet.Tables.Add(DataTable2)
DataAdapter.Fill(DataTable2)
GridControl2.DataSource = DataTable2
DataSet.Relations.Add(DataTable1.Columns("ID"), DataTable2.Columns("TASK"))
Here is the result
http://img11.hostingpics.net/pics/4713062013102413h1253.png
As you can see, I added manually a relationship between the two data tables but it doesn't really do what i'm looking for. It simply adds a subline in the first grid control.
http://img11.hostingpics.net/pics/7508382013102413h1309.png
I tried to apply what PeterG suggested but I still can't make it work.
Dim SQLQuery As String = "SELECT * FROM SCHEME.ARCH_TASKS"
Command = New OracleCommand(SQLQuery, Conn)
DataAdapter = New OracleDataAdapter(Command)
DataSet.Tables.Add(DataTable1)
DataAdapter.Fill(DataTable1)
SQLQuery = "SELECT * FROM SCHEME.ARCH_LINK_ROLE_TASK"
Command = New OracleCommand(SQLQuery, Conn)
DataAdapter = New OracleDataAdapter(Command)
DataSet.Tables.Add(DataTable2)
DataAdapter.Fill(DataTable2)
DataSet.Relations.Add(DataTable1.Columns("ID"), DataTable2.Columns("TASK"))
BindingSource1.DataSource = DataSet
GridControl1.DataSource = BindingSource1.DataSource
GridControl1.DataMember = "Table1"
BindingSource2.DataSource = BindingSource1.DataSource
GridControl2.DataSource = BindingSource2.DataSource
GridControl2.DataMember = "Table2"
Am I missing something ?
Thank you.

Loading datagridview in VB.NET programmatically using dataset

I've created a datagridview, dataGridReport in the Designer view of VB 2010.
I'm then using a query to fill a dataset, and I want this dataset to populate the datagridview... but it doesn't seem to work.
This is my code:
Dim con As New OleDb.OleDbConnection
con.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Users\Administrator\Documents\MenuDB.accdb"
con.Open()
Dim ds As DataSet = New DataSet
Dim adapter As New OleDb.OleDbDataAdapter
Dim sql As String
sql = #"SELECT OrderDate, MenuItem
FROM MenuItems, Orders
WHERE Orders.itemID = MenuItems.ID
AND Format(Orders.OrderDate,'mm/dd/yyyy') >= #" + fromDate + "#
AND Format(Orders.OrderDate,'mm/dd/yyyy') <= #" + toDate + "#"
adapter.SelectCommand = New OleDb.OleDbCommand(sql, con)
adapter.Fill(ds)
dataGridReport.DataSource = ds
I know something's missing now - I've looked around and most people seem to use something like dataGridReport.DataSource = ds.Tables('somereference')
But mine is a dynamically created dataset from a query joining two tables, it's not something stored among the project Data Sources. I also haven't bound the datagridview to any datasource via its properties. What am I missing?
(By the way, the sql query is correct, I've tested it and returns expected results).
You can get the table from the data source via the index:
dataGridReport.DataSource = ds.Tables(0)
dataGridReport.DataSource = ds.Tables("Tablename")
Dim con As New OleDb.OleDbConnection
con.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;
DataSource=C:\Users\Administrator\Documents\MenuDB.accdb"
con.Open()
Dim ds As DataSet = New DataSet
Dim adapter As New OleDb.OleDbDataAdapter
Dim sql As String
sql = #"SELECT OrderDate, MenuItem
FROM MenuItems, Orders
WHERE Orders.itemID = MenuItems.ID
AND Format(Orders.OrderDate,'mm/dd/yyyy') >= #" + fromDate + "#
AND Format(Orders.OrderDate,'mm/dd/yyyy') <= #" + toDate + "#"
adapter.SelectCommand = New OleDb.OleDbCommand(sql, con)
adapter.Fill(ds,sql)
dataGridReport.DataSource = ds.tables(sql)

aspx vb.net checkboxlist to be populated from sql server

can you populate checkboxlist from sql server query like a dropdownlist? with autopostback=true? i am using vb.net and have 50 checkboxes that shall show up from the database data depending on the selected value of the previous dropdownlist. also can i change the label of the checkbox each time the value is from DB? the label shall be same as the checkbox value.
Assuming your CheckBoxList's ControlId is myCheckBoxList:
Dim mySQL As String = "Name_of_stored_proceedure"
Dim mySqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection("The_connection_string")
Dim mySqlCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(mySQL, mySqlConnection)
mySqlCommand.CommandType = CommandType.StoredProcedure
Dim myDataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(mySqlCommand)
Dim myDataTable As New DataTable
mySqlConnection.Open()
myDataAdapter.Fill(myDataTable)
mySqlConnection.Close()
myCheckBoxList.DataSource = myDataTable
myCheckBoxList.DataBind()
This is the process to use a stored procedure. If you would like to use straight SQL or a parametrized query take out the mySqlCommand.CommandType = CommandType.StoredProcedure and put the SQL in for the "Name_of_stored_proceedure".
Remember to put the column name from the database in the DataValueField property of the CheckBoxList that you would like to for the the value and the column name for the DataTextField you would like to use for the text.