I want to show my SQL Server error to the user. When a method returns a value there is no problem with this code.
But when there is no record I want to give an error message which gets retrieved from a reader.
Here is what I tried:
Controller:
public IActionResult Index()
{
//It works ok when there is a retrieved date
var expenses = context.GetAll(GetUserId());
//I think it`s like this. But it says there is a problem with view.
foreach (var item in expenses)
{
if (item.Error != null)
{
ViewBag.Error = item.Error;
return View();
}
}
return View(expenses);
}
I get this error:
System.ArgumentNullException: Value cannot be null.
Parameter name: source .
In my index view where I say:
#if (Model.Any())
{
html...
}
else
{
Viewbag.Error
}
C# method:
public IQueryable<Expenses> GetAll(string UserId)
{
List<Expenses> expenses = new List<Expenses>();
using (DALC.GetConnection())
{
DALC.Command("spGetUserExpenses");
DALC.cmd.Parameters.Add("#UserId", SqlDbType.NVarChar).Value = UserId;
using (SqlDataReader reader = DALC.cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Expenses entities = new Expenses
{
Id = Convert.ToInt32(reader["Id"]),
TotalAmount = Convert.ToDouble(reader["Amount"]),
Desription = reader["Notes"].ToString(),
Date = Convert.ToDateTime(reader["Date"]),
IsCash = Convert.ToBoolean(reader["IsCash"]),
IsCard = Convert.ToBoolean(reader["IsCard"])
};
expenses.Add(entities);
}
}
if (reader.NextResult())
{
reader.Read();
//setting second select statement which is an error by my side?
expenses.Add(new Expenses { Error = reader[0].ToString() });
}
}
}
return expenses.AsQueryable();
}
Stored procedure:
ALTER PROCEDURE [dbo].[spGetUserExpenses]
#UserId nvarchar(450)
AS
BEGIN
SELECT
e.ID,
e.Amount,
e.Notes,
e.[Date],
e.IsCash,
e.IsCard
FROM
Expenses e
WHERE
UserId = #UserId
AND DATENAME(YEAR, CAST(e.[Date] AS varchar)) = DATENAME(YEAR, GETDATE())
AND DATENAME(MONTH, CAST(e.[Date] AS varchar)) = DATENAME(MONTH, GETDATE())
ORDER BY
e.ID DESC
IF (##ROWCOUNT = 0)
BEGIN
SELECT N'No Expense Found'
END
END
I prefer this way to select data from stored procedures, it works for me
connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand com = new SqlCommand("spGetUserExpenses", connection);
com.CommandType = CommandType.StoredProcedure;
//CARA 1
if (userid == null)
{
com.Parameters.AddWithValue("#UserId", DBNull.Value)
}
else
{
com.Parameters.AddWithValue("#UserId", UserId)
}
IDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Expenses entities = new Expenses()
{
Id = Convert.ToInt32(reader["Id"]),
TotalAmount = Convert.ToDouble(reader["Amount"]),
Desription = reader["Notes"].ToString(),
Date = Convert.ToDateTime(reader["Date"]),
IsCash = Convert.ToBoolean(reader["IsCash"]),
IsCard = Convert.ToBoolean(reader["IsCard"])
};
expenses.Add(entities);
}
reader.Close();
connection.Close();
return expenses;
Here are all the Queries I should be able to retrive and display corresponding data. I need help with the sytax and code. All this should be displayed to another xml when user enters query button.
/*1a. Retrieve full details of all suppliers; that is, make a list of all attributes used to describe each supplier for every supplier in s.
SELECT * FROM s;
1b. Retrieve full details of all parts.
SELECT * FROM p;
1c. Retrieve full details of all suppliers-of-parts. (Hint This query should test the vertical and horizontal scrolling capabilities of your app!)
SELECT * FROM sp;
2. Retrieve sno and sname for all suppliers based in "London".
SELECT sno,s.sname FROM s WHERE ( city = 'London' );
3. Is part "P07" still manufactured in "London"?
SELECT pno,city FROM p WHERE pno = 'P07';
4. What is the pno and pname of all parts that cost more than $1.00?
SELECT pno,pname FROM p
WHERE ( cost > 1.00 );
5. Retrieve full details for parts supplied by supplier "S01".
SELECT p.pno,p.pname,p.cost,p.city FROM p,sp
WHERE ( (p.pno = sp.pno) AND (sp.sno = 'S01') );
6. Retrieve a list of each distinct part city. Does this eliminate duplicate cities from the list?
SELECT DISTINCT city FROM p;
7. Retrieve the pno, pname, and city of parts manufactured in "Dallas", "Paris", or "London".
SELECT pno,pname,city FROM p WHERE ( city IN ('Dallas','Paris','London') );
8. Retrieve the pno and pname of each part and sno and sname of all suppliers who supply qty ≥ 100 (100 or more) of any one part.
SELECT DISTINCT p.pno,p.pname,s.sno,s.sname FROM s,p,sp
WHERE ( (s.sno = sp.sno) AND (p.pno = sp.pno) AND (sp.qty >= 100) );
9. Retrieve sname of all suppliers who supply less than 400 of pno = "P02", but only if the part and the supplier have identical city attributes.
SELECT s.sname FROM s,p,sp
WHERE ( (s.sno = sp.sno) AND (p.pno = sp.pno) AND
(sp.pno = 'P02') AND (sp.qty < 400) AND (p.city = s.city)
);
10. What is the pno of parts with pname = "Nut" or pname = "Screw"?
SELECT pno FROM p WHERE ( pname IN ('Nut','Screw') ); */
Here is my code so far
Main activity.java
package com.rough.problem.problem9;
import android.content.Context;
import android.database.sqlite.SQLiteStatement;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText Select;
DatabaseHelper myDB;
Button create, query, quit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new DatabaseHelper(this);
// Select = (EditText) findViewById(R.id.SelectStatement);
create = (Button) findViewById(R.id.Create);
query = (Button) findViewById(R.id.Query);
quit = (Button) findViewById(R.id.Quit);
// Select.getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tableS tables1 = new tableS("S01", "Smith", "London");
tableS tables2 = new tableS("S02", "Jones", "Paris");
tableS tables3 = new tableS("S03", "Blake", "Paris");
tableS tables4 = new tableS("S04", "Clark", "London");
tableS tables5 = new tableS("S05", "Adams", "Athens");
tableS tables6 = new tableS("S06", "Gracia", "Austin");
tableS tables7 = new tableS("S07", "Thomas", "Paris");
tableS tables8 = new tableS("S08", "Jonas", "New York");
tableS tables9 = new tableS("S09", "Zeeman", "Dallas");
tableP tablep1 = new tableP("P01","Nut", "0.13", "London");
tableP tablep2 = new tableP("P02","Bolt", "0.25", "Paris");
tableP tablep3 = new tableP("P03","Screw", "0.09", "Rome");
tableP tablep4 = new tableP("P04","Screw", "0.10", "London");
tableP tablep5 = new tableP("P05","Cam", "5.26", "Paris");
tableP tablep6 = new tableP("P06","Cog", "3.75", "London");
tableP tablep7 = new tableP("P07","Washer", "0.11", "El Paso");
tableP tablep8 = new tableP("P08","Nut", "0.13", "Austin");
tableP tablep9 = new tableP("P09","Nail", "0.01", "Rome");
tableP tablep10 = new tableP("P10","Gear", "9.99", "Houston");
tableP tablep11 = new tableP("P11","Tack", "0.01", "Dallas");
tableP tablep12 = new tableP("P12","Wheel", "2.30", "Paris");
tableSP tablesp1 = new tableSP("S01", "P01","300");
tableSP tablesp2 = new tableSP("S01", "P02","200");
tableSP tablesp3 = new tableSP("S01", "P03","400");
tableSP tablesp4 = new tableSP("S01", "P04","200");
tableSP tablesp5 = new tableSP("S01", "P05","100");
tableSP tablesp6 = new tableSP("S01", "P06","100");
tableSP tablesp7 = new tableSP("S01", "P12","50");
tableSP tablesp8 = new tableSP("S02", "P01","300");
tableSP tablesp9 = new tableSP("S02", "P02","400");
tableSP tablesp10 = new tableSP("S03", "P02","200");
tableSP tablesp11 = new tableSP("S04", "P02","200");
tableSP tablesp12 = new tableSP("S04", "P04","300");
tableSP tablesp13 = new tableSP("S04", "P05","400");
tableSP tablesp14 = new tableSP("S05", "P01","50");
tableSP tablesp15 = new tableSP("S05", "P09","150");
tableSP tablesp16 = new tableSP("S05", "P011","320");
tableSP tablesp17 = new tableSP("S06", "P02","150");
tableSP tablesp18 = new tableSP("S06", "P12","825");
tableSP tablesp19 = new tableSP("S08", "P08","180");
tableSP tablesp20 = new tableSP("S08", "P11","250");
tableSP tablesp21 = new tableSP("S09", "P01","100");
tableSP tablesp22 = new tableSP("S09", "P03","200");
tableSP tablesp23 = new tableSP("S09", "P04","100");
tableSP tablesp24 = new tableSP("S09", "P07","300");
tableSP tablesp25 = new tableSP("S09", "P10","100");
tableSP tablesp26 = new tableSP("S09", "P12","200");
// add them
myDB.insertdata(tables1);
myDB.insertdata(tables2);
myDB.insertdata(tables3);
myDB.insertdata(tables4);
myDB.insertdata(tables5);
myDB.insertdata(tables6);
myDB.insertdata(tables7);
myDB.insertdata(tables8);
myDB.insertdata(tables9);
myDB.insertdatap(tablep1);
myDB.insertdatap(tablep2);
myDB.insertdatap(tablep3);
myDB.insertdatap(tablep4);
myDB.insertdatap(tablep5);
myDB.insertdatap(tablep6);
myDB.insertdatap(tablep7);
myDB.insertdatap(tablep8);
myDB.insertdatap(tablep9);
myDB.insertdatap(tablep10);
myDB.insertdatap(tablep11);
myDB.insertdatap(tablep12);
myDB.insertdatasp(tablesp1);
myDB.insertdatasp(tablesp2);
myDB.insertdatasp(tablesp3);
myDB.insertdatasp(tablesp4);
myDB.insertdatasp(tablesp5);
myDB.insertdatasp(tablesp6);
myDB.insertdatasp(tablesp7);
myDB.insertdatasp(tablesp8);
myDB.insertdatasp(tablesp9);
myDB.insertdatasp(tablesp10);
myDB.insertdatasp(tablesp11);
myDB.insertdatasp(tablesp12);
myDB.insertdatasp(tablesp13);
myDB.insertdatasp(tablesp14);
myDB.insertdatasp(tablesp15);
myDB.insertdatasp(tablesp16);
myDB.insertdatasp(tablesp17);
myDB.insertdatasp(tablesp18);
myDB.insertdatasp(tablesp19);
myDB.insertdatasp(tablesp20);
myDB.insertdatasp(tablesp21);
myDB.insertdatasp(tablesp22);
myDB.insertdatasp(tablesp23);
myDB.insertdatasp(tablesp24);
myDB.insertdatasp(tablesp25);
myDB.insertdatasp(tablesp26);
}
});
List<tableS> tables = myDB.alltableS();
if (tables != null) {
String[] itemsNames = new String[tables.size()];
for (int i = 0; i < tables.size(); i++) {
itemsNames[i] = tables.get(i).toString();
}
// display like string instances
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, itemsNames));
}
}
//-----------------------------------------------------------
public void ClickQueryButton(View view)
//-----------------------------------------------------------
{
String select = ((EditText) findViewById(R.id.SelectStatement)).getText().toString();
DatabaseHelper mydb = openOrCreateDatabase("SPdb1.db", Context.MODE_PRIVATE, null);
try
{
SQLiteStatement statement = myDB.compileStatement(query);
Intent intent = new Intent(this,ExecuteQueryActivity.class);
intent.putExtra(QUERYX, query);
startActivity(intent);
} catch (SQLiteException exception)
{
Toast.makeText(this, "SQL query contains syntax error", Toast.LENGTH_LONG).show();
}
}
public boolean onCreateOptionMenu (Menu menu){
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
return true;
}
}
DatabaseHelper.java
package com.rough.problem.problem9;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "SPdb1.db";
public static final String TABLE_NAME = "s";
public static final String COL_1 = "sno";
public static final String COL_2 = "sname";
public static final String COL_3 = "city";
public static final String TABLENAME = "p";
public static final String COL1 = "pno";
public static final String COL2 = "pname";
public static final String COL3 = "cost";
public static final String COL4 = "city";
public static final String TABLENAMES = "sp";
public static final String COLS1 = "sno";
public static final String COLS2 = "pno";
public static final String COLS3 = "qty";
private static final String[] COLUMNS = {COL_1, COL_2, COL_3};
private static final String[] COLUMN = {COL1,COL2,COL3,COL4};
private static final String[] COLUMNSS = {COLS1,COL2,COLS3};
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String query = "Create Table IF NOT EXISTS " + TABLE_NAME + "(" +
COL_1 + " TEXT PRIMARY KEY COLLATE NOCASE," + //<<<<<<<<<< ADDED SPACE
COL_2 + " TEXT NOT NULL COLLATE NOCASE," + //<<<<<<<<<< ADDED SPACE
COL_3 + " TEXT NOT NULL COLLATE NOCASE" + //<<<<<<<<<< ADDED SPACE
");";
db.execSQL(query);
String querys = "Create Table IF NOT EXISTS " + TABLENAME + "(" +
COL1 + " TEXT NOT NULL COLLATE NOCASE," + //<<<<<<<<<< ADDED SPACE
COL2 + " TEXT NOT NULL COLLATE NOCASE," + //<<<<<<<<<< ADDED SPACE
COL3 + " REAL NOT NULL," + //<<<<<<<<<< ADDED SPACE
COL4 + " TEXT NOT NULL COLLATE NOCASE," + //<<<<<<<<<< ADDED SPACE
"PRIMARY KEY (pno)" + //<<<<<<<<<< ADDED SPACE
");";
db.execSQL(querys);
String queryss = "Create Table IF NOT EXISTS " + TABLENAMES + "(" +
COLS1 + " TEXT NOT NULL REFERENCES s(sno) COLLATE NOCASE," + //<<<<<<<<<< ADDED SPACE
COLS2 + " TEXT NOT NULL COLLATE NOCASE," + //<<<<<<<<<< ADDED SPACE
COLS3 + " INTEGER NOT NULL," + //<<<<<<<<<< ADDED SPACE
"FOREIGN KEY (pno) REFERENCES p(pno)" + //<<<<<<<<<< ADDED SPACE
");";
db.execSQL(queryss);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// you can implement here migration process
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLENAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLENAMES);
onCreate(db);
}
public void insertdata (tableS tables){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_1, tables.getSno());
values.put(COL_2, tables.getSname());
values.put(COL_3,tables.getCity());
// insert
db.insert(TABLE_NAME, null, values);
db.close();
}
public void insertdatap ( tableP tablep){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values1 = new ContentValues();
values1.put(COL1, tablep.getPno());
values1.put(COL2, tablep.getPname());
values1.put(COL3, tablep.getCost());
values1.put(COL4, tablep.getCity());
// insert
db.insert(TABLENAME,null,values1);
db.close();
}
public void insertdatasp ( tableSP tablesp){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values2 = new ContentValues();
values2.put(COLS1,tablesp.getSno());
values2.put(COLS2,tablesp.getPno());
values2.put(COLS3,tablesp.getQty());
// insert
db.insert(TABLENAMES,null,values2);
db.close();
}
public List<tableS> alltableS() {
List<tableS> tables = new LinkedList<tableS>();
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
tableS Tables = null;
if (cursor.moveToFirst()) {
do {
Tables = new tableS();
Tables.setSno(cursor.getString(0));
Tables.setSname(cursor.getString(1));
Tables.setCity((cursor.getString(2)));
tables.add(Tables);
} while (cursor.moveToNext());
}
return tables;
}
}
And finally tableS.java I am not attaching tableP.java and tableSP.java as they are very similar to tableS.java
package com.rough.problem.problem9;
public class tableS {
private String sno;
private String sname;
private String city;
public tableS() {
}
public tableS(String sno , String sname, String city) {
this.sno = sno;
this.sname = sname;
this.city = city;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
#Override
public String toString() {
return sno + " \t\t " + sname + " \t\t " + city ;
}
}
The way that I would determine the syntax is via a combination of :-
reading the documentation e.g SQL As Understood By SQLite SELECT
Perhaps testing the queries outside of the App using one of the available SQLite Management tools e.g. DB Browser for SQLite, Navicat etc..
You would initially build and populate a test Database
You could then run the queries.
Ideally you would the convert these queries to utilise the convenience methods (query for selections if appropriate (sometimes the convenience methods may not be able to cope in which case you may rarely have to utilise the rawQuery method)).
I would suggest that trying to display the result from 10 queries with various lists would be a UI nightmare. So perhaps you should consider displaying the various information in different activities or in different fragments of an activity.
Assuming Navicat as the SQLite Management tool then you could :-
Start the Navicat program (after installing).
Click on File/New Connection.
Provide a suitable Connection Name e.g. SPdb1
Click the New SQLite 3 radio button.
Click on the ... at the right of the Database File input, select an appropriate location for the Database and enter the SPdb1.db as the database name.
Click Save and the OK.
Double click the connection and the double click main.
You could build the tables by click on New Table or you could write queries to create them (I tend to prefer the latter).
You could then populate them using appropriate data either using the options with the tables or via queries.
An alternative would be to copy the database from the App using Android Studio's Device Explorer to a suitable location and then create a connection using an existing database.
You can now copy the proposed queries (either as a single query or multiple queries (Navicat allows/captures results from multiple queries)) and try running them to check the syntax and functionality. e.g. Using the database form your previous question (an then adding the p and sp tables) the results are that all 10 queries run without any syntax errors as per :-
SELECT * FROM s
> OK
> Time: 0s
SELECT * FROM p
> OK
> Time: 0s
SELECT * FROM sp
> OK
> Time: 0s
SELECT sno,s.sname FROM s WHERE ( city = 'London' )
> OK
> Time: 0s
SELECT pno,city FROM p WHERE pno = 'P07'
> OK
> Time: 0s
SELECT pno,pname FROM p
WHERE ( cost > 1.00 )
> OK
> Time: 0s
SELECT p.pno,p.pname,p.cost,p.city FROM p,sp
WHERE ( (p.pno = sp.pno) AND (sp.sno = 'S01') )
> OK
> Time: 0s
SELECT DISTINCT city FROM p
> OK
> Time: 0s
SELECT pno,pname,city FROM p WHERE ( city IN ('Dallas','Paris','London') )
> OK
> Time: 0s
SELECT DISTINCT p.pno,p.pname,s.sno,s.sname FROM s,p,sp
WHERE ( (s.sno = sp.sno) AND (p.pno = sp.pno) AND (sp.qty >= 100) )
> OK
> Time: 0s
SELECT s.sname FROM s,p,sp
WHERE ( (s.sno = sp.sno) AND (p.pno = sp.pno) AND
(sp.pno = 'P02') AND (sp.qty < 400) AND (p.city = s.city)
)
> OK
> Time: 0s
SELECT pno FROM p WHERE ( pname IN ('Nut','Screw') )
> OK
> Time: 0s
However only the first and fourth query results in any data being extracted as per :-
Converting queries
Once you are assured that the queries work as expected then you can convert these into methods, perhaps within you DatabaseHelper class.
Taking the most complicated query :-
SELECT s.sname FROM s,p,sp
WHERE ( (s.sno = sp.sno) AND (p.pno = sp.pno) AND
(sp.pno = 'P02') AND (sp.qty < 400) AND (p.city = s.city)
);
Look at the SQLiteDatabase query method (the simplest of the 4) SQLiteDatabase - query
This takes 7 parameters as per :-
The tablename.
The columns to be returned in the cursor as a String array (null for all i.e, equivalenet to *).
The selection (WHERE clause less WHERE) (null for no where clause).
The selection arguments to replace the placeholders in the selection (i.e. each ? is replaced by an argument) (null for no arguments).
The GROUP BY column(s) as a string (null for no GROUP BY clause).
The HAVING clause, as a string, less the HAVING keyword (null for no HAVING clause).
The ORDER BY clause as a string, less the ORDER BY keywords.
The tablename is used to specify JOINED tables.
So :-
- 1 will be FROM s,p,sp less theFROM keyword, so s,p,sp
e.g. :-
String table = "s,p,sp";
2 will be a sinlge element string array with the element as the string s.sname
e.g. :-
String[] columns = new String[]{"s.sname"};
3 will be WHERE ( (s.sno = sp.sno) AND (p.pno = sp.pno) AND
(sp.pno = 'P02') AND (sp.qty < 400) AND (p.city = s.city)
) less the WHERE clause and with the parameters as ? marks
e.g. :-
String whereclause = "( (s.sno = sp.sno) AND (p.pno = sp.pno) AND (sp.pno = '?') AND (sp.qty < ?) AND (p.city = s.city));
4 The arguments will be p02 and 400 respectively (however you may want to pass these so the values can be changed so here the assumption is that values will be passed via the call to the method as variables partno (string) and quantity (integer))
so :- String[] whereargs = new String[]{partno,String.valueOf(quantity};
5 will be null as there is no GROUP BY clause.
6 will be null as there is no HAVING clause.
7 will be null as there is no ORDER BY clause.
As such the resultant method could be :-
public Cursor getLowUsageSupplierNamesForAPart(String partno, int Quantity) {
String table = "s,p,sp";
String[] columns = new String[]{"s.sname"};
String whereclause = "( (s.sno = sp.sno) AND (p.pno = sp.pno) AND (sp.pno = '?') AND (sp.qty < ?) AND (p.city = s.city));
String[] whereargs = new String[]{partno,String.valueOf(quantity};
SQLiteDatbase db = this.getWriteableDatabase();
return db.query(
table,
columns.
whereclause,
whereargs,
null,null,null
);
}
You could then call this from an activity/fragment that instantiates or has an instantiated DatabaseHelper ( e.g. MyDB in your MainActivity) using :-
mCursor = MyDB.getLowUsageSupplierNamesForAPart("p02",400);
You could then display the list of supplier names in a ListView (noting that you should close mCursor when done with the activity e.g. by overidding the onDestroy method to include mCursor.close().
Note this example assumes that mCursor has been defined as a class variable and thus has ample scope.)
We are using Dapper.net for the data layer in an ASP.net MVC 5 app.
One operation requires the use of the MERGE command (if the TitleID exists, update the record, if it doesn't, insert it) - something like this:
MERGE BookInventory bi
USING BookOrder bo
ON bi.TitleID = bo.TitleID
WHEN MATCHED THEN
UPDATE
SET bi.Quantity = bi.Quantity + bo.Quantity
WHEN NOT MATCHED BY TARGET THEN
INSERT (TitleID, Title, Quantity)
VALUES (bo.TitleID, bo.Title,bo.Quantity);
Can Dapper be used to map values onto this MERGE statement?
I can't find anything on this and it's unclear what the best approach to using MERGE with Dapper.net is?
Just found out that you can do it like I did in .NET Fiddle code here: https://dotnetfiddle.net/e2G3Ho
Pasting the code below
// #nuget: Dapper -Version 1.60.6
using Dapper;
using System;
using System.Data.SqlClient;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class OrderDetail
{
public int OrderDetailID { get; set; }
public int OrderID { get; set; }
public int ProductID { get; set; }
public int Quantity { get; set; }
}
public static void Main()
{
string sql = "SELECT * FROM OrderDetails";
string sql2 = #"MERGE INTO OrderDetails AS TARGET
USING (
VALUES
(#OrderDetailID, #OrderID, #ProductID, #Quantity)
) AS SOURCE (OrderDetailID, OrderID, ProductID, Quantity)
ON SOURCE.OrderDetailID = TARGET.OrderDetailID
WHEN MATCHED THEN
UPDATE SET Quantity = 666
WHEN NOT MATCHED THEN
INSERT (OrderID, ProductID, Quantity)
VALUES (SOURCE.OrderID, SOURCE.ProductID, SOURCE.Quantity);
";
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
{
List<OrderDetail> details = new List<OrderDetail> {
{new OrderDetail() {OrderDetailID = 1, OrderID = 1024, ProductID = 42, Quantity = 1000} },
{new OrderDetail() {OrderDetailID = 9999, OrderID = 10268, ProductID = 42, Quantity = 1000} }
};
connection.Execute(sql2, details);
var orderDetails = connection.Query<OrderDetail>(sql).ToList();
FiddleHelper.WriteTable(orderDetails);
}
}
}
Untested, but this should do the trick:
const string sql = #"
merge into SomeTable as Target
using (select #myId AS id) as Source
on (Target.id = Source.id)
when matched then
update set Target.SomeColumn = #myValue
when not matched by Target then
insert (SomeColumn) values (#myValue)";
conn.Execute(sql, new { myId = 999, myValue = 123 })
I don't know how to assign value return from stored procedure into web api? how to pass variables througn angulrjs
I called serivice
var promisePost = crudService.candidatePost(Candidates);
promisePost.then(function (pl) {
alert("Sucessfully Inserted")
}, function (err) {
alert("NOt Inserted")
});
my app.js is
MyApp.service('crudService', function ($http, RESOURCES) {
this.candidatePost = function (Candidate) {
var request = $http({
method: "post",
url: RESOURCES.baseUrl + "saveCandidate",
data: Candidate
});
return request;
}
});
my controller is
[HttpPost]
[Route("api/saveCandidate")]
public HttpResponseMessage AddDetail(Candidate ct)
{
SqlConnection con = new SqlConnection(Constant.ConnectionString);
SqlCommand cmd = new SqlCommand();
int rowInserted = 0;
try
{
cmd = new SqlCommand("sp_Insert_tblCandidate", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Name", ct.Name);
cmd.Parameters.AddWithValue("#Gender", ct.Gender);
cmd.Parameters.AddWithValue("#Dob", ct.Dob);
cmd.Parameters.AddWithValue("#Mob", ct.Mob);
cmd.Parameters.AddWithValue("#EntryDate", ct.EntryDate);
cmd.Parameters.AddWithValue("#Note", ct.Note);
cmd.Parameters.AddWithValue("#Emial", ct.Emial);
cmd.Parameters.AddWithValue("#Address", ct.Address);
rowInserted = cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception obj)
{
if (rowInserted != 1)
{
var message = obj.Message;// string.Format("Insertion Of Data is not Succefully Executed");
HttpError err = new HttpError();
return Request.CreateResponse(HttpStatusCode.NotFound, err);
}
}
finally
{
if (con.State == System.Data.ConnectionState.Open)
{
con.Close();
}
}
var alertmessage = string.Format("Insertion Of Data is Succefully Executed");
return Request.CreateResponse(HttpStatusCode.OK, alertmessage);
}
My stored Procedure is
IF EXISTS ( SELECT *
FROM sysobjects
WHERE id = object_id(N'[dbo].[sp_Insert_tblCandidate]')
and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
DROP PROCEDURE [dbo].[sp_Insert_tblCandidate]
END
GO
Create procedure [dbo].[sp_Insert_tblCandidate]
(#Name varchar(50) ,
#Gender char(1),
#dob varchar(25),
#Mob varchar(15),
#EntryDate varchar(50),
#Note varchar(100),
#Emial varchar(50),
#Address varchar(50)
)As
Begin
INSERT INTO [dbo].[tblCandidate] VALUES (#Name,#Gender,#dob,#Mob,#EntryDate,#Note,#Emial,#Address);
SELECT SCOPE_IDENTITY() as CandidateId;
end
GO
My stored procedure purpose is insert data & return last inserted id. In this table id set to auto increment.Anyone can help me?
You are returning the new Id so you shouldn't be using
cmd.ExecuteNonQuery()
But something that can handle the return value. Try changing it to
rowInserted = (int) cmd.ExecuteScalar();
Also right now you are checking in the catch the rowInserted which doesn't makes much sense.
You cannot get a result from ExecuteNonQuery() as it only returns the number of rows affected for the query you just executed.
Since you are returning the SCOPE_IDENTITY(), you can either use ExecuteReader() or ExecuteScalar().
rowInserted = (int)cmd.ExecuteScalar();
or
using(SqlDataReader rdr = cmd.ExecuteReader())
{
while(rdr.Read())
{
rowInserted = Convert.ToInt32(rdr[0]);
}
}
But using SqlDataReader is kinda unnecessary in this scenario.
Plus, just an observation; be careful when choosing between SCOPE_IDENTITY(), ##IDENTITY and IDENT_CURRENT.