How to Update one table by another table datas using storedprocedure? - c#

I have a Table named Dummywith marks and student id (3 mark fields are there). I have another one table Applicantdetails this table also contains mark fields. what I want to do is I want to updates Dummy tables marks to Applicantdetails table's marks as per student id. I want to do this by mssql Storedprocedure. Any way to achieve it. If we write in code wise it should be like this
qry="select Applicantid,mark1,mark2,mark3 from Dummy"
//saved result to Datatable dt
foreaach(DataRow. rows in dt.rows)
{
string id=Convert.ToString(row["ApplicantID"].tostring();
string mark1=Convert.ToString(row["ApplicantID"].tostring();
string mark2=Convert.ToString(row["ApplicantID"].tostring();
string mark3=Convert.ToString(row["ApplicantID"].tostring();
qry="update Applicantdetails set Mark1=mark1,Mark2=Mark2,Mark3=Mark3
where ApplicantID=id";
}
This format I want to bring in storedprocedure..Please help me

Your Stored procedure would receive #applicationid ? Otherwise it would update all, Create a stored procedure with the following SQL
UPDATE A
SET Mark1= d.mark1, Mark2 = d.mark2, Mark3= d.mark3
FROM ApplicationDetail A
JOIN Dummy d on d.Applicationid = A.Applicationid

Related

checking the value in a table in asp.net

let me I have a table called to test and another table called testTwo testTwo.
I got a value (row) from the table test which is rollNo. Now Ii want to check and compare that if there is any row called rollNo rollNo exists in my testTwo testTwo table.
How can Ii do this in asp.net? Thanks.
I am adding some sample code here to get the concept that what Ii am trying to do.
what can be the way that Ii can compare my existing value with the existing table that the testTwo the tabletable has the row ( rollNO)
If your second table is DataTable, you can write something like below.
DataTable table=testTwo;//table example
DataColumnCollection columns = table.Columns;
if (columns.Contains("rollNO"))
{
....
}

In Data table first column displaying with tablename.columnname in c#

When i am running query in data base and fill data table with load method first column name coming with table name.column name(Employee.Name,sal,location) rest of the column name coming only column names present in the database.Why table name appending first column name while displaying in data table structure please help me in this regards.
DataTable table = new DataTable();
try
{
using (IDbCommand DbCommand = dbConnection.CreateCommand())
{
DbCommand.CommandText = query;
IDataReader reader = DbCommand.ExecuteReader();
table.Load(reader);
}
}
Sample query Select Employee.Numnber, Employee.salary, Employee.city,dept.deptId from Employee inner join dept where deptiId=EmployeeDeptId
The actual query is :
Select
AP_LINE.LINE_NO, AP_LINE.PRODUCT, AP_LINE.QTY, AP_LINE.REQUESTED_DATE,
AP_LINE.LIST_PRICE, AP_LINE.CONS_NET_MULT,AP_LINE.NETADDERS,
AP_LINE.CONS_NET_PRICE, AP_LINE.Details,TDP_JSP.ID, TDP_JSP.LINE_NO,
TDP_JSP_AppInfo.Id
From AP_LINE, CONFIG, TDP_JSP
inner join TDP_JSP_AppInfo on TDP_JSP.ID= TDP_JSP_AppInfo.ID
where AP_LINE.LINE_NO = TDP_JSP.LINE_NO ;
Out put in data table as coming below
Ap_Line.Line_No,Product,QTY,Requested Date...
There are two LINE_NO columns in the query, AP_LINE.LINE_NO and TDP_JSP.LINE_NO. Columns must have unique names, so DataTable.Load used the two-part name wherever needed. It that wasn't possible it would start appending indexes or even generate names like Column0, Column1 etc.
In this case though, one of those columns isn't needed. The two tables are join those columns. WHERE AP_LINE.LINE_NO = TDP_JSP.LINE_NO is the old-style, frowned-upon join syntax. Those two columns will always have the same data so one of them can be removed.

Modify Ms-Access Table Structure

I have an access table with certain structure and I want to copy it to a table with a different structure. The structure of the original table is:
col1-ddate(datetime)
col2-type(text)
col3-vvalue(number).
Each 3 rows have the same date. The type has repeated 3 values(sys, dia, pul).
Now I want to insert this table data to a new(temporary) table which has the following structure
col1-ddate(datetime)
col2-sys(number)
col3-dia(number)
col4-Pul(number)
How can I do that?
If I've understood correctly, a SQL query using conditional aggregation should achieve what you are looking for, grouping by the datetime field, e.g.:
select
col1-ddate,
max(iif(col2-type = "sys", col3-vvalue, null)) as col2-sys,
max(iif(col2-type = "dia", col3-vvalue, null)) as col3-dia,
max(iif(col2-type = "pul", col3-vvalue, null)) as col4-pul
into
NewTemporaryTable
from
YourTable
group by
col1-ddate
Change YourTable to the name of your existing table.

ASP.NET How to Generate a Student Number with Formula (e.g. RS 1001000)

I need to generate a student number as the formula below with ASP.NET.
So every time a student is added to the database it should automatically generate a student ID.
NOTE( when adding a new student to the database I need to know the last student ID that has been added to the database so it can follow the correct formula).
RS 1011000
RS 1011001
RS 1011002
RS 1011003
RS 1011004
Create an id column as bigint identity(10000000, 1) and a calculated column which prefixes 'RS' to that column.
So you would get something like
Create table example (studentId bigint not null identity(10000000, 1),
RsNumber as 'RS' + CAST(studentId as varchar(15)))
http://www.kodyaz.com/articles/sql-server-computed-column-calculated-column-sample.aspx

How to delete selected row on datagridview and that also reflects to the database?

I am working with C# Window Form, I have created a form connect to mySQL database and it display list of databases, list of tables on each database and also tables contents of each table.
Questions that I have here:
After I selected a random cell in the table (datagridview) and click Delete button, I want that row (corresponding to the selected cell) to be deleted on the database.
I also need that the datagridview table content also will be refreshed and updated (with that row has been removed). (This part I think I can do if I know how to do the part 1)
So I need help with the question 1, one of those things I can't not figure out is that how can I write the SQL statement to put in the SQLadapter or SQLcommandbuilder or whatever it is. I know the SQL statement should be like:
Delete from (selected Table) Where (THIS IS THE PART WHERE I STUCK AT) => I dont know what to put in this condition, how to get it?
Any helps and advises is really appreciated!
The delete statement should consider all the selected table primary key columns and the selected row from the datagridview.
How to get the primary key columns:
SELECT `COLUMN_NAME`
FROM `information_schema`.`COLUMNS`
WHERE (`TABLE_SCHEMA` = 'dbName')
AND (`TABLE_NAME` = 'tableName')
AND (`COLUMN_KEY` = 'PRI');
Source: A better way to get Primary Key columns
How your delete statement should look like:
DELETE FROM <TABLE>
WHERE <PRIMARY_KEY_COLUMN_1> = <ROW_VALUE_1>
AND <PRIMARY_KEY_COLUMN_2> = <ROW_VALUE_2>
You see, the table could have multiple columns uniquely identifying a row. There is also the possibility of existing a reference for that very row on another table, which would prevent you from deleting it.
It would look like this:
List<string> primaryKeyColumns = GetPrimaryKeyColumns(SelectedDB, SelectedTable);
string deleteWhereClause = string.Empty;
foreach (string column in primaryKeyColumns)
{
DataGridViewRow row = datagridview.CurrentCell.OwningRow;
string value = row.Cells[column].Value.ToString();
if (string.IsNullOrEmpty(deleteWhereClause))
{
deleteWhereClause = string.Concat(column, "=", value);
}
else
{
deleteWhereClause += string.Concat(" AND ", column, "=", value);
}
}
string deleteStatement = string.Format("DELETE FROM {0} WHERE {1}", SelectedTable, deleteWhereClause);
The method GetPrimaryKeyColumns returns the names of all the primary key columns of the selected table using the select statement i posted.
You would also have to deal with other types of columns such as dates and strings, but that's basically what you will have.

Categories