My code as follows:
namespace EntityDAO
{
public static class StudentDAO
{
public static Boolean AddStudent(StudentDTO oDto)
{
string str =System.Configuration.ConfigurationManager.AppSettings["myconn"];
SqlConnection oconnection = new SqlConnection(str);
oconnection.Open();
try
{
string addstring = "insert into STUDENT(ID,NAME)values('"
+ oDto.ID + "','"
+ oDto.NAME + "')";
SqlCommand ocommand = new SqlCommand(addstring,oconnection);
ocommand.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
oconnection.Close();
}
but when I run this program ,an error message has been occured and the error message for oconnection.Open(); and the message is 'InvalidOperationException'(Instance failure).I have tried many times to solve this problem but i did't overcome this problem.so please,anyone help me.
The following is not proposed as a complete solution to your problem, but should help you figure it out:
namespace EntityDAO
{
public static class StudentDAO
{
public static Boolean AddStudent(StudentDTO oDto)
{
var str = ConfigurationManager.AppSettings["myconn"];
using (var oconnection = new SqlConnection(str))
{
oconnection.Open();
try
{
var addstring = string.Format(
"insert into STUDENT(ID,NAME)values('{0}','{1}')", oDto.ID, oDto.NAME);
using (var ocommand = new SqlCommand(addstring, oconnection))
{
ocommand.ExecuteNonQuery();
}
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}
}
}
}
}
Don't ever hide exceptions from yourself. Even if the caller of this code wants true or false, make sure you log the details of the exception.
Also, what AYK said about SQL Injection. I'm entering this as CW, so if someone has more time than I do, they should feel free to edit to use parameters.
Related
I have this program, that I have to handle the exceptions for, but I never done it so I'm kind of confused. I'm guessing I'd have to handle exceptions like the value in the textfile is empty, there is no index, there is no "=" or the file is empty, but I don't really know how to define them or where to put them. Here is my code:
(the lines in the text file should look like this:
EMPIS_MAG_BUDOW=12
EMPIS_DESKA_FASOLKA=2
SM_PORTAL_POL1=-4
)
using System;
using FirebirdSql.Data.FirebirdClient;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace dokselect
{
class indexstan
{
public string index;
public double standysp;
}
class WynikPorownania
{
public string Indeks;
public int Ilosc;
public override string ToString()
{
return Indeks + " : " + Ilosc;
}
}
}
class Program
{
public static void Main()
{
///////CONNECTION
string conn = "database=C:/PCBiznes/BAZA/IXION2_LOGMAG.FB;user=SYSDBA;password=masterkey;DataSource=192.168.24.112;Port=3050";
FbConnection myConnection = new FbConnection(conn);
FbDataReader myReader = null;
string sql = "select KARTOTEKA.indeks, STANMAG.standysp FROM kartoteka JOIN stanmag using(ID_KARTOTEKA);";
FbCommand myCommand = new FbCommand(sql, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
///////LIST lista1
List<indexstan> listadb = new List<indexstan>();
double standysp;
string index;
while (myReader.Read())
{
index = myReader[0].ToString();
standysp = Convert.ToDouble(myReader[1]);
listadb.Add(new indexstan { index=index, standysp=standysp });
//Console.WriteLine(myReader[0].ToString());
}
myConnection.Close();
Console.WriteLine(listadb.Count);
//RETURN STANDYSP FUNCTION
double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex).standysp;
return result;
}
//zwr("EMPIS_DESKA_FASOLKA");
//READ FROM TXT AND RETURN HIGHER
string path = "C:/Users/Praktykant/Documents/textdocs/dok1.txt";
List<WynikPorownania> listatf = File.ReadAllLines(path).Select(line =>
{
var linia = line.Split("=");
string index = linia[0];
int value = int.Parse(linia[1]);
return new WynikPorownania {Indeks = index, Ilosc = (int)Math.Max(value, zwr(index))};
}).ToList();
//DISPLAY ALL LISTATF CLASSES
foreach(object WynikPorownania in listatf)
{
Console.WriteLine(WynikPorownania);
}
}
}
}
I tried to make an exception like this, if the value is not given but it doesn't work and the program still collapses when the value in the text file is empty
List<WynikPorownania> listatf = File.ReadAllLines(path).Select(line =>
{
var linia = line.Split("=");
string index = linia[0];
if (linia[1] == "")
{
throw new Exception("Is empty ... or whatever you caught");
return null;
}
else
{
int value = int.Parse(linia[1]);
}
return new WynikPorownania { Indeks = index, Ilosc = (int)Math.Max(value, zwr(index)) };
}).ToList();
To handle exceptions in C# you should use try catch (see c# refence try-catch-finally)
In your code I suggest to wrap a try catch around the firebird connection, since you are depending on this:
try {
// your code
} catch (Exception ex) {
// define what happens if exception is thrown
}
Its key that you include for instance the firebird connection and the while loop into the try, since those are things your code is depending on.
To handle the value in the text file:
//DISPLAY ALL LISTATF CLASSES
// note that I have replaced the "object" with your class.
foreach(WynikPorownania wynikPorownania in listatf)
{
if (wynikPorownania.Indeks != ... || wynikPorownania.Ilosc != ... )
throw new Exception("Is empty ... or whatever you caught");
Console.WriteLine(wynikPorownania);
}
//try something like below.
class Program
{
public static void Main()
{
//CONNECTION
try
{
//... <YOUR CODE>
//DISPLAY ALL LISTATF CLASSES
foreach(object WynikPorownania in listatf)
{
Console.WriteLine(WynikPorownania);
/*if <your condition>
{
throw <your exception1>
}
else if <your condition>
{
throw <your exception1>
}*/
}
}
catch (Exception <your Exception>)
{
// Throw the message
// Or return the code
}
}
}
I am creating a simple inventory system using c#.
When I am generating the invoice number, the form is loaded but it doesn't show anything.
It is an auto-incremented invoice number; order is completed incrementally by 1.
For example, starting at E-0000001, after order we expect E-0000002. I don't understand why it is blank.
No error displayed. I tried to debug the code but I couldn't find what's wrong.
public void invoiceno()
{
try
{
string c;
sql = "SELECT MAX(invoid) FROM sales";
cmd = new SqlCommand(sql, con);
var maxInvId = cmd.ExecuteScalar() as string;
if (maxInvId == null)
{
label4.Text = "E-000001";
}
else
{
int intVal = int.Parse(maxInvId.Substring(2, 6));
intVal++;
label4.Text = String.Format("E-{0:000000}", intVal);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Write(ex.StackTrace);
}
}
Let's extract a method - NextInvoiceId - we
Open connection
Execute query
Obtain next invoice number
Code:
private int NextInvoiceNumber() {
//TODO: put the right connection string here
using(var conn = new SqlConnection(ConnectionStringHere)) {
conn.Open();
string sql =
#"SELECT MAX(invoid)
FROM sales";
using (var cmd = new SqlCommand(sql, conn)) {
var raw = cmd.ExecuteScalar() as string;
return raw == null
? 1 // no invoces, we start from 1
: int.Parse(raw.Trim('e', 'E', '-')) + 1;
}
}
}
Then we can easily call it:
public void invoiceno() {
label4.Text = $"E-{NextInvoiceNumber():d6}";
}
Edit: You should not swallow exceptions:
try
{
...
}
// Don't do this!
catch (Exception ex) // All the exceptions will be caught and...
{
// printed on the Console...
// Which is invisible to you, since you develop Win Forms (WPF) application
Console.WriteLine(ex.ToString());
Console.Write(ex.StackTrace);
}
let system die and inform you that something got wrong
I'm getting a thrown exception when attempting to import a SqlDataRecord with a DateTime2 data type. There is no exception thrown in this snippet, but the cause of the later exception is evident in the following code.
using System;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
namespace Testing123
{
public class Program
{
public static object SQLTParser(SqlMetaData smd, string val)
{
TypeCode systc = Parameter.ConvertDbTypeToTypeCode(smd.DbType);
try
{
return Convert.ChangeType(val, systc);
}
catch (Exception ex)
{
if (ex is InvalidCastException || ex is FormatException || ex is OverflowException)
{
Console.WriteLine("Exception reached casting " + val + " to " + Type.GetType("System." + Enum.GetName(typeof(TypeCode), systc)) + ": " + ex.Message + ex.ToString()); //smd.GetType().Name
return null;
}
else
{
Console.WriteLine("Null value exception");
return null;
}
}
}
public static void Main()
{
SqlMetaData sqmd = new SqlMetaData("dt", SqlDbType.DateTime2, 27, 7);
SqlDataRecord sdr = new SqlDataRecord(sqmd);
sdr.SetValue(0, SQLTParser(sqmd, "2017-01-12 01:23:12.3456789"));
//set BreakPoint
//sdr -> Non-Public members -> _columnMetaData[0] -> Precision = 27
//sdr -> Non-Public members -> _columnSmiMetaData[0] -> Non-Public members -> Precision = 0
}
}
}
As noted, if you set the breakpoint and watches as indicated in MS Visual Studio*, Precision does not match between columnMetaData and columnSmiMetaData, which by the second such entry (!!) throws an exception:
Metadata for field 'dt' of record '2' did not match the original record's metadata.
which matches the exception thrown by
line 3755 of ValueUtilsSmi
due to the return of MetadataUtilsSmi.IsCompatible
Essentially, precision in the field's metadata of record 2 doesn't match that which is in the SmiMetaData of record 1. In record 1, the MD and SMD don't match either, but based on the logic Microsoft was using for an IEnumerator'd SqlDataRecord, it doesn't become an issue until the 2nd record.
Is this a MS bug? Or is there a way to force the precision value of the SmiMetaData? Or ignore that particular field check in ValueUtilsSmi? Specifying SqlMetaData sqmd = new SqlMetaData("dt", SqlDbType.DateTime2, 0, 7); allows the parsing to proceed, but drops sub-second precision from the data.
Here is, in a nutshell, how I'm attempting to send the data to a database. Apologies if this portion is not a complete example.
public class FullStreamingDataRecord : IEnumerable<SqlDataRecord>
{
private string _filePath;
public bool _hasHeader { get; private set; }
private ParserDict _pd; //notably has colStructure which is an array of SqlMetaData[]
public FullStreamingDataRecord(string FilePath, ParserDict pd)
{
_filePath = FilePath;
_pd = pd;
_hasHeader = true;
}
public static object SQLTParser(SqlMetaData smd, string val)
{
TypeCode systc = Parameter.ConvertDbTypeToTypeCode(smd.DbType);
try
{
return Convert.ChangeType(val, systc);
}
catch (Exception ex)
{
if (ex is InvalidCastException || ex is FormatException || ex is OverflowException)
{
Console.WriteLine("Exception reached casting " + val + " to " + Type.GetType("System."+Enum.GetName(typeof(TypeCode),systc)) + ": " + ex.Message + ex.ToString()); //smd.GetType().Name
return null; //smd.TParse(val);
}
else
{
Console.WriteLine("Null value exception casting...attempting a different method");
return null; smd.TParse(val);
}
}
}
public IEnumerator<SqlDataRecord> GetEnumerator()
{
int len = this._pd.colStructure.Length;
StreamReader fileReader = null;
try
{
using (fileReader = new StreamReader(this._filePath))
{
string inputRow = "";
string[] inputColumns = new string[len];
if (_hasHeader && !fileReader.EndOfStream)
{
inputRow = fileReader.ReadLine(); //and ignore
}
while (!fileReader.EndOfStream)
{
string temp = "";
inputRow = fileReader.ReadLine();
inputColumns = inputRow.Split(new char[]{','},len);
SqlDataRecord dataRecord = this._pd.colStructure
for (int j = 0; j < len; j++) { // i = counter for input columns
string currentKey = this._pd.colStructure[j].SqlMetaData.Name;
string curval = inputColumns[j];
var ty = this._pd.colStructure[j].SqlMetaData; //.DbType;
dataRecord.SetValue(j, SQLTParser(ty, curval));
// dataRecord.GetSqlMetaData(j).Adjust(dataRecord.GetValue(j));
}
yield return dataRecord;
}
}
}
// no catch block allowed due to the "yield" command
finally
{
fileReader.Close();
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
Which I call with this in my Main()
using (SqlConnection conn = new DBConnect().conn)
{
conn.Open();
SqlCommand importProc = new SqlCommand("tvp_"+pd.tableDestination, conn);
importProc.CommandType = CommandType.StoredProcedure;
importProc.CommandTimeout = 300;
SqlParameter importTable = new SqlParameter();
importTable.ParameterName = "#ImportTable";
importTable.TypeName = "dbo.tt_"+pd.tableDestination;
importTable.SqlDbType = SqlDbType.Structured;
importTable.Value = new FullStreamingDataRecord(fn, pd);
importProc.Parameters.Add(importTable);
importProc.ExecuteNonQuery(); //this line throws the exception
}
Apologies for not Console.WriteLineing the values of these watches.
Unfortunately, it seems that these parameters are protected against examination using Reflection's sdr.GetType().GetProperties(), even with the appropriate BindingFlags set. But at least you can see the values in debug mode!
I am trying to return an error message for a method in my code, but I am unable to find the correct return object, I have searched on the internet but have no been successful in finding an answer.
The following is a code snippet:
public ArrayList getBeneficiaryID(string UserID)
{
try
{
//long Beneficiary_ID = 0;
//string BeneficiaryID = "";
ArrayList BeneficiaryArray = new ArrayList();
// Open connection to the database
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["Databasebcfintec_alice"].ConnectionString;
aConn = new SqlConnection(strConn);
aConn.Open();
// Set up a command with the given query and associate
// this with the current connection.
string sql = "Select BeneficiaryID from Beneficiary where user_id = '" + UserID + "'";
cmd = new SqlCommand(sql);
cmd.Connection = aConn;
// Execute the query
odtr = cmd.ExecuteReader();
while (odtr.Read())
{
BeneficiaryArray.Add(odtr["BeneficiaryID"]);
//User_ID = (long)(odtr["user_id"]);
//UserID = User_ID.ToString();
}
odtr.Close();
return BeneficiaryArray;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
//return ex.ToString();
return;
}
}
Error Message:
"An object of a type convertible to arraylist is required"
Another method I tried was using the following code:
return ex.ToString();
but it provided the following error message:
"Cannot implicitly convert type 'string' to
'System.Collections.ArrayList'
You can just put return null; or return new ArrayList();
since it looks like you don't care with what you're gonna get with the catch. You already have a console log.
The problem is that you have to return an object. You can't do
return;
because you're not returning anything. You also can't return a string because it can't be cast to an ArrayList.
You can, however, return null, which it sounds like is what you want.
In the catch section you should have to throw the exception.
public ArrayList MethodName(string UserID)
{
try
{
//write your code
}
catch (Exception )
{
//log the exception
throw ;
}
}
I have a web method function has checks if a name exists in the database but I am getting the error:
Error 114 'lookups_Creditor.CheckIfNameExists(string)': not all code
paths return a value
Here is the web method:
[WebMethod]
public static bool CheckIfNameExists(string Name)//error on this line
{
try
{
Creditor.CheckIfNameCreditorExists(Company.Current.CompanyID, Name);
}
catch (Exception ex)
{
}
}
And here is the search function for the sql:
public static string CheckIfNameCreditorExists(int CompanyID, string Name)
{
DataSet ds = new DataSet();
string accNo = "";
string sql = "proc_CheckIfACCreditorExists";
string query = "SELECT c.* " +
" FROM Creditor c " +
" WHERE c.Company_ID = " + CompanyID + " AND c.Name LIKE '" + Name + "' ";
DataTable dt = new DataTable();
using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(query, DataUtils.ConnectionStrings["TAT"]))
{
adapter.SelectCommand.CommandType = CommandType.Text;
adapter.SelectCommand.CommandText = query;
adapter.Fill(dt);
if (dt.Rows.Count > 0)
{
accNo = Convert.ToString(dt.Rows[0]["AccoutCode"]);
}
}
return accNo;
}
I am trying to create a method that searches for the name in the database. If the name exists, then return the account code associated with that name. I will the display a message on the screen telling the user that the name already exists on the account ABC.
[WebMethod]
public static bool CheckIfNameExists(string Name)//error on this line
{
bool Result = false;
try
{
Result = Creditor.CheckIfNameCreditorExists(Company.Current.CompanyID, Name) != "";
}
catch (Exception ex)
{
}
return Result
}
You have written the return type as Bool and you are not returning anything.
If you don't have anything to return then just make that return type to Void.
By the method name it indicates you should return either "True" or "False".
The error just indicates that, you should return something when you have a return type other than void in your methods.
Your method is supposed to return bool, yet you don't return anything.
You need to rewrite it something like this:
[WebMethod]
public static bool CheckIfNameExists(string Name)
{
bool res = false;
try
{
// Check your string result if it's null or empty
// and store the result in local variable
res = !string.IsNullOrEmpty(Creditor.CheckIfNameCreditorExists(Company.Current.CompanyID, Name));
}
catch (Exception ex)
{
// Do your handling here
}
return res;
}