how to get data from excel sheet with specific code - asp.net

I have one problem with my code I can not achieving mobile no from my code :
OleDbCommand cmdoledb = new OleDbCommand("Select [Phone(R)] from [nil$] where [Code]='" + treadcode + "' ", oledbConn);
mobileno = cmdoledb.ExecuteScalar().ToString();
string message = "Dear " + clientname + " Your BSE Trade Position: " + Codemsg + " Ledger balance is : " + purebalance + ". (Aadinath)";
Int64 len = message.Length;
if (!string.IsNullOrEmpty(mobileno.Trim()))
{
dsmsg.Tables[0].Rows.Add(true, message, len, mobileno, clientname, treadcode);
}
else
{
dsmsg.Tables[0].Rows.Add(true, message, len,"No Mobile Found", clientname, treadcode);
}
this is my excel sheet screen shot :

Related

Referenced foreign key id is null when inserting new values in the second table. How do I simply select the id from the first table to the second?

I have created two tables in my Database Manager
#Override
public void onConfigure(SQLiteDatabase db) {
db.setForeignKeyConstraintsEnabled(true);
super.onConfigure(db);
}
#Override
public void onCreate(SQLiteDatabase database)
{
database.setForeignKeyConstraintsEnabled(true);
// create table
String sql = "create table "
+ TABLE_NAME + " ("
+ C_LIST_ID + " integer primary key autoincrement, "
+ C_LIST_NAME + " VARCHAR(255))";
Log.d(TAG, "in onCreate");
//create second table
String sql2 = "create table "
+ TABLE_NAME_ITEMS + " ("
+ C_LIST_ITEM + " VARCHAR(255), "
+ C_DATE + " text, "
+ C_FLAG + " integer, "
+ C_LIST_NAME + " VARCHAR(255), "
+ C_ITEM_ID + " integer, foreign key "
+ "("+C_ITEM_ID+")" + " references " +TABLE_NAME+ "("+C_LIST_ID+"))";
Log.d(TAG, "in onCreate second database");
database.execSQL(sql);
database.execSQL(sql2);
}
This is how I am inserting the data into the second table. In the tables themselves, the foreign key IDs are null. How do I bring the id from the first table to the second when creating a new item with the same C_LIST_NAME?
public void insertItemData(String item, String listName)
{
Integer listNameId = getListId(listName);
database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(C_LIST_ITEM, item + listNameId);
contentValues.put(C_LIST_NAME, listName);
database.insertOrThrow(TABLE_NAME_ITEMS, null , contentValues);
Log.d(TAG, "insertData: data has been inserted in items");
}
I should've rested last night after trying to figure this out for hours.
I figured it out and the solution was very simple. I was just too tired to think I guess.
In case anyone is curious, I just need to insert it like this:
public void insertItemData(String item, String itemDescription, String listName)
{
baseActivity = new BaseActivity();
database = getWritableDatabase();
database.execSQL("INSERT INTO "+TABLE_NAME_ITEMS+
"("+C_ITEM_NAME+"," +C_ITEM_DESCRIPTION+ ", " +C_LIST_NAME+ "," +C_DATE+ "," +C_FLAG+ "," +C_ITEM_ID+ ")"
+ "VALUES ('"+item+ "', '"+itemDescription+ "', '" +listName+ "', '" +baseActivity.getCurTime()+ "', '" +0+ "', "
+ "(SELECT " +C_LIST_ID+ " FROM " +TABLE_NAME+ " WHERE "
+C_LIST_NAME+ "=?))",
new Object[]{listName});
Log.d(TAG, "insertData: data has been inserted in items");
}

How to save textfile data in column format in ASP.NET MVC

I'm currently taking input from a registration form and download the input detail as a text file. the downloaded textfile result is : JimJone03/09/1998 00:00:00xxx#gmail.com 230436315
I want the result to be displayed in column format.
For example:
Jim Jone
03/09/98
xxx#gmail.com
+23057xxx556
This is what I have tried:
public ActionResult Create(Information information)
{
var byteArray = Encoding.ASCII.GetBytes(information.FirstName + ""
+ information.Surname + "" + information.DOB + ""
+ information.Email + " " + information.Tel);
var stream = new MemoryStream(byteArray);
return File(stream, "text/plain", "your_file_name.txt");
}
You can try to use regex to match them:
var input = "Jim Jone 03/09/98 xxx#gmail.com +23057897765";
var name = new Regex("^[a-zA-Z\\s]+").Match(input);
var birthday = new Regex("\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}").Match(input);
var email = new Regex(#"([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)").Match(input);
var tel = new Regex("\\+\\d+").Match(input);
Console.WriteLine("Input: " + "\"" + input + "\"");
Console.WriteLine("Name: " + "\"" + name.Value.Substring(0, name.Value.Length - 1) + "\"");
Console.WriteLine("Birthday: " + "\"" + birthday.Value + "\"");
Console.WriteLine("Email: " + "\"" + email.Value + "\"");
Console.WriteLine("Tel: " + "\"" + tel.Value + "\"");
Output:
Then, you can write them all to your text file line-by-line.
Why don't you just use Environment.NewLine?
var byteArray = Encoding.ASCII.GetBytes($"{information.FirstName}{Environment.NewLine}{information.Surname}{Environment.NewLine}{information.DOB}{Environment.NewLine}{information.Email}{Environment.NewLine}{information.Tel}");
This should add a 'new line' after each value in your file.

Android Sqlite Create syntax error

I can't seem to find out what exactly I did wrong here. Seem to be getting a syntax error in my create command inside the createNewTable method.
Android SQLite Insert Error (Syntax error code 1)
I've added my database code:
private static final String KEY_DATE = "date";
private static final String KEY_TIME_IN = "timeCheckIn";
private static final String KEY_TIME_OUT = "timeCheckOut";
private static final String KEY_LATITUDE_IN = "latCheckIn";
private static final String KEY_LONGITUDE_IN = "longCheckIn";
private static final String KEY_LATITUDE_OUT = "latCheckOut";
private static final String KEY_LONGITUDE_OUT = "longCheckOut";
public void createNewTable(){
SQLiteDatabase db = this.getWritableDatabase(int _id);
Log.v("Database", "Adding Check in table: " + _id);
String CREATE_TABLE = " CREATE TABLE " + Integer.toString(_id) + "("
+ KEY_DATE + " TEXT," + KEY_TIME_IN + " TEXT," + KEY_TIME_OUT + " TEXT,"
+ KEY_LATITUDE_IN + " REAL," + KEY_LONGITUDE_IN + " REAL," + KEY_LATITUDE_OUT + " REAL,"
+ KEY_LONGITUDE_OUT + " REAL" + ")";
db.execSQL(CREATE_TABLE);}
My logcat error is as follows:
Caused by: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: CREATE TABLE 1(date TEXT,timeCheckIn TEXT,timeCheckOut TEXT,latCheckIn REAL,longCheckIn REAL,latCheckOut REAL,longCheckOut REAL)
You have a table name that starts with a number. Not sure if SQLite allows it, but in MySQL, if your table name is a number, then for every query, you need to specify the table number with double quotes. Try giving the table name a prefix
SQLite is telling you unescaped table names are not allowed to start with a number, in this case the number 1. If you want to use numbers you must escape the table name with brackets, like so
String CREATE_TABLE = " CREATE TABLE [" + Integer.toString(_id) + "] ("
+ KEY_DATE + " TEXT," + KEY_TIME_IN + " TEXT," + KEY_TIME_OUT + " TEXT,"
+ KEY_LATITUDE_IN + " REAL," + KEY_LONGITUDE_IN + " REAL," + KEY_LATITUDE_OUT + " REAL,"
+ KEY_LONGITUDE_OUT + " REAL" + ")";

Log Parser c# error using STRCAT with CASE

I'm having trouble with the log parser, punctually on the use of the function STRCAT parameter with CASE, using log parser the query works perfectly and using a simple STRCAT without CASE the query works even using c#, the problem starts when i use CASE. Am I missing something?
Here's the error:
CLogQueryClass: Error 8007064f: Execute: error parsing query: Syntax Error: : cannot find closing parenthesys for function STRCAT [ SQL query syntax invalid or unsupported. ]
string query = "SELECT " + " STRCAT('" + entry.Name +"'";
query += #", CASE INDEX_OF(SUBSTR(cs-uri-stem,1), '/')
WHEN 'NULL' THEN 'DEFAULTAPPPOOL'
ELSE EXTRACT_TOKEN(cs-uri-stem,1,'/')
END";
query += ") AS APPPOOL";
query += ", '" + Environment.MachineName + "' as server";
query += ", '" + entry.Name + "' as site";
query += ", cs-uri-stem as csUriStem";
query += ", c-ip as cIp, sc-status as scStatus";
query += ", sc-bytes as scBytes";
query += ", cs-bytes as csBytes";
query += ", time-taken as timeTaken";
query += " FROM " + logAddress + "\\" + yesterdayLogName;
// Initialize a LogQuery object
logQuery = new LogQueryClass();
logRecordSet = logQuery.Execute(query,new COMIISW3CInputContextClass());
//SHOWS RESULT
for (; !logRecordSet.atEnd(); logRecordSet.moveNext())
{
logrecord = logRecordSet.getRecord();
int i = 0;
while (i < 9)
{
Console.WriteLine(logrecord.getValue(i));
i++;
}
Thanks
First, it looks like you are mixing types. The CASE INDEX_OF(SUBSTR(cs-uri-stem,1), '/') WHEN 'NULL' compares an integer to string. This should be:
CASE INDEX_OF(SUBSTR(cs-uri-stem,1), '/')
WHEN NULL THEN 'DEFAULTAPPPOOL'
ELSE EXTRACT_TOKEN(cs-uri-stem,1,'/')
END
The error complains about finding the close parenthesis, but I've found that parsing errors can result in misleading error messages with LogParser.
Second, I've tested the following in C# targeted at .NET 3.5 (4.0 had an issue with embedded type. Similar to this...):
string logAddress = "C:\\Path\\to\\consolidatedFile";
string entryName = "blah";
string yesterdayLogName = "fileName.log";
string query = "SELECT " + " STRCAT('" + entryName + "'"
+ ", CASE INDEX_OF(SUBSTR(cs-uri-stem,1), '/') "
+ "WHEN NULL THEN 'DEFAULTAPPPOOL' "
+ "ELSE EXTRACT_TOKEN(cs-uri-stem,1,'/') "
+ "END"
+ ") AS APPPOOL"
+ ", '" + Environment.MachineName + "' as server"
+ ", '" + entryName + "' as site"
+ ", cs-uri-stem as csUriStem"
+ ", c-ip as cIp, sc-status as scStatus"
+ ", sc-bytes as scBytes"
+ ", cs-bytes as csBytes"
+ ", time-taken as timeTaken"
+ " FROM " + logAddress + "\\" + yesterdayLogName;
// Initialize a LogQuery object
COMW3CInputContextClassClass ctx = new COMW3CInputContextClassClass();
//LogQueryClass logQuery = new LogQueryClass();
LogQueryClass logQuery = new LogQueryClassClass();
//ILogRecordset logRecordSet = logQuery.Execute(query, new COMIISW3CInputContextClass());
ILogRecordset logRecordSet = logQuery.Execute(query, ctx);
//SHOWS RESULT
for (; !logRecordSet.atEnd(); logRecordSet.moveNext())
{
ILogRecord logrecord = logRecordSet.getRecord();
int i = 0;
while (i < 9)
{
Console.WriteLine(logrecord.getValue(i));
i++;
}
}
This ran successfully and return results. I commented out the lines initially presented since when I used them nothing returned on the console. That might be a difference of the code not presented. Finally, I substituted a string entryName for the entry.Name object assuming that it returns a string.
I hope this helps.

how to use combination of different fields in query when no of fields are uncertain?

I'm using advance search option in library project
Here is idea :
i have 6 different fields to allow search if i give the option for user to enter value in any of 6 option or enter combined fields how to use sql query to retrieve the value.
For example the fields are author, publication, price, subject, edition, bookid
and if user enter only one value i could search but if user enter more than one if i try combinations then there are many combination.
Please suggest me how to define the query?
you can do something like..
string strFilters = string.Empty;
if (author != "" )
{
strFilters += " Author = " + yourAuthorString + " and ";
}
if (publication != "")
{
strFilters += " publication = " + yourpublicationString + " and ";
}
if (price != "")
{
strFilters += " price = " + priceValue + " and ";
}
if (subject != "")
{
strFilters += " subject = " + yoursubjectString + " and ";
}
if (edition != "")
{
strFilters += " edition = " + youreditionString + " and ";
}
if (strFilters.Length > 3)
{
strFilters = strFilters.Remove(strFilters.Length - 5, 5);
}

Resources