Retrieve Integer value from string in a Gridview - c#

I have a gridview, when inserting data into the table dept(100, 1, 'IT') and when fetch the data from two tables emp and dept the records look like below grid:
EMpNo DeptNo DeptName
Harshal 1 IT
My problem is that, when I click on a grid view all records shows in respected text boxes
but issue is, I got all data except empno. Here I get as Name of employee instead of name I want to Empno.

you can try like this
select empno,deptno,deptname from emp inner join dept on emp.dept.id=dept.id

Related

add column in table1 derived calculation from table2 's column

I have two tables
Employee
Finance
Employee contains 3 columns
id
Name
BaseSalary
Finance table contains 2 columns
id
BonusPercentage
I want to add TotalBonus column in Employee table. For calculation I am using BaseSalary from Employee table and BonusPercentage from Finance table.
TotalBonus = BaseSalary + (BaseSalary * BonusPercentage)
I can use this with stored procedure but i don't want. However, I created scalar function (calculateBonus) which returns TotalBonus but the problem is that i can't add column in table as i have dependency of BonusPercentage of Finance table.
In general i want employee table to have following:
id
Name
BaseSalary
TotalBonus
I know there are many ways but want in Employee table as I am using this table on C#.
Thanks
Assuming that there can be a maximum of one row in finance for each employee, you want a (left) join:
SELECT e.id,
e.name,
e.basesalary,
coalesce(f.bonuspercentage, 0) * e.basesalary totalbonus
FROM employee e
LEFT JOIN finance f
ON f.id = e.id;

Auto populating textbox with incremented column value

I have a dropdownlist of ID=ddlDept which shows the department from a column named DId and table named Department. Under that selected department there are students with StudentID from the column named StudentID and table named registration which i have done by inner join operation. Now I want to auto populate a textbox in a aspx webform with the incremented StudentID by selecting from ddlDept.
Before selecting ddlDept Suppose i had ddlDept of 02 and the last studentID in that "02" department is 0705044.
Now if I select "02" from ddlDept then the textbox for StudentID must populate with the value of 0705045
Also if the Student ID is 0405089 then by selecting the ddlDept it must populate with 0405089.
That means increment will be of the last 2 digits only.
I am very stuck.Suggest with sample code.Thnx in advance

Textboxes using BindingSource with more tables

On my form there is a listbox containing movie titles from table1 and if you select one from the listbox you can see all the movie datas in textboxes bindingsourced from table1.
However i have another table containing + data about the selected movie but i don"t know how can i set the textbox to that because if i simply select the bindingsource from the table2 it won't recognise the movie somehow i need to connect them ...
Can you help me with a solution?
this is how i added the table1 so when user type in textbox it can filter listbox and select the movie
var h = from s in db.Filmek where s.Filmcim.StartsWith(textBox1.Text) select s;
filmekBindingSource1.DataSource = h;
But i need the connected data from table2 to show in the textbox too
Have you tried a join between the two tables?
First create a class for the result of the two tables joined, something like:
public class MovieResult
{
public int MovieId {get;set;}
public string MovieTitle {get;set;}
//Other properties of table one
//Properties of table two
}
Then modify your query to look something like:
var result = from movie in db.Filmek
where movie.Filmcim.StartsWith(textBox1.Text)
join anotherTable in db.OtherTable on movie.FilmId equals anotherTable.FilmId
selecte new MovieResult
{
MovieId = movie.Id,
MovieTitle = movie.Title,
//Fill the properties of table one
//Fill the properties of table two, ex. if table two contains a field named Country
Country = anotherTable.CountryName //etc
}
Finally bind it to the result:
filmekBindingSource1.DataSource = result;

fix data property name in gridview

I have 2 table :
table1: name, family, cityID
table2: cityID, cityName
I have a gridview that it's datasource is:
select * from table1, table2 where table1.cityID = table2.cityID
I disable autoCreatColumns
I add 3 columns to my datagrid.
in first column I fix the data property name to name
in second column I fix the data property name to family
but in third colum when I fix the data propery name to table2.cityName It dosent work...
I want to show the city name in third column.
Its an example but in fact, I have cityName in anouther table that I used, and if I use just cityName, makes error
can any body help me?
Thanks...
try to change
select * from table1, table2 where table1.cityID = table2.cityID
To
select table1.name as Name,table1.family as Family,table2.cityName as City from table1, table2 where table1.cityID = table2.cityID
SELECT name,family,cityName from table1 INNER JOIN table2 ON table1.cityID = table2.cityID
As you are doing inner joins with city ids on both tables..U will get matched Table IDs with name,citynames
You don't need to write table2.cityName, just write cityName and it should work.

How to get Sum from two tables?

i have two tables first one name is "sales" and second one name is "items"
in both tables have same columns "code" and " qtd ";
i want write MYSQL query witch i need sum(qtd) from both of tables where code is same in both of tables .
for single table i am using this
"select code,sum(qtd) from sales group by code";
Try this:
Select code, sum(qtd)
from (
select code, qtd from sales
union all
select code, qtd from items) as innerTable
group by code

Categories