Retrieving records from a database in C# 2008 - c#

I need syntax help with the following code logic:
I have a code block that gets email address from the database. The email addresses need to be assigned to a string variable strEmailAddress with a comma seperation
My code is:
SqlConnection conn = new SqlConnection(strConn);
string sqlEmailAddress = "usp_Get_Email_Address";
SqlCommand cmdEmailAddr = new SqlCommand(sqlEmailAddress, conn);
cmdEmailAddr.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader sqlDREmailAddr = cmdEmailAddr.ExecuteReader();
How can I loop through the records and store the results in strEmailAddress seperated by comma?

while (sqlDREmailAddr.Read())
{
//...process each row here
}
I would also wrap the reader in a using statement to make sure it is closed properly:
using (SqlDataReader sqlDREmailAddr = cmdEmailAddr.ExecuteReader())
{
}
Depending on what the columns in your dataset is named, reading values from each record will look something like this (update: now with all addresses merged):
var emailAddress = new StringBuilder();
var emailAddressOrdinal = sqlDREmailAddr.GetOrdinal("EmailAddress");
while (sqlDREmailAddr.Read())
{
if (emailAddress.Length > 0)
emailAddress.Append(',');
emailAddress.Append(sqlDREmailAddr.GetString(emailAddressOrdinal));
}

Use the SqlDataReader.Read method:
while (sqlDREmailAddr.Read())
{
...
// Assumes only one column is returned with the email address
strEmailAddress = sqlDREmailAddr.GetString(0);
}

while (sqlDREmailAddr.Read())
{
// handle row here
}

This is what you're looking for....
using (SqlConnection conn = new SqlConnection(strConn)){
string sqlEmailAddress = "usp_Get_Email_Address";
using (SqlCommand cmdEmailAddr = new SqlCommand(sqlEmailAddress, conn)){
cmdEmailAddr.CommandType = CommandType.StoredProcedure;
conn.Open(); // Typo Glitch!
using (SqlDataReader sqlDREmailAddr = cmdEmailAddr.ExecuteReader()){
while(sqlDREmailAddr.Read()){
if (!sqlDREmailAddr.IsDBNull(sqlDREmailAddr.GetOrdinal("emailAddr"))){
// HANDLE THE DB NULL...
}else{
strEmailAddress = sqlDREmailAddr.GetSqlString(sqlDREmailAddr.GetOrdinal("emailAddr"));
// Do something with strEmailAddr...
}
}
}
}
}
Notice:
A typo glitch on the conn variable...
A check is made to ensure that the Database value returned is not NULL
A call is made to GetOrdinal to return the column based on emailAddr string value that corresponds to the column from the query for SQL Select...which is an int type) as the parameter for GetSqlString..
Edit: Thanks to John Saunders for pointing out a blooper!
Edit#2: Thanks to Peter Lillevold for pointing out a mis-spelling...
Hope this helps,
Best regards,
Tom.

Related

Need to select a value from table and store it into variable

Hello everyone I am currently working on some testing project and I am having a little problem. Using selenium, I need to SendKey in specific element but instead of fixed value i need to use value (data) from my database. Can anyone help me with how to retrieve single value from database and store it in a variable so i can use it later.
Thank you and sorry for a noobish question - see code below:
SqlConnection conn = new SqlConnection();
SqlCommand command;
SqlDataReader dataReader;
conn.ConnectionString = "Server=******;Database=****;User ID=sqlserver;password=****;MultipleActiveResultSets=true;");
string query = "select RequestID, from AutomaticPayment where RequestID ='1230322'";
DataTable dt = new DataTable();
command = new SqlCommand(query, conn);
conn.Open();
dataReader = command.ExecuteReader();
dt.Load(dataReader);
driver.FindElement(By.Id("requestID")).SendKeys(VALUE FROM DATABASE);
You can use the following code
using (SqlConnection connection = new SqlConnection(_connectionString))
{
SqlDataAdapter sda = new SqlDataAdapter(query, connection);
connection.Open();
SqlCommand cmd = new SqlCommand(query, connection);
try
{
result = cmd.ExecuteScalar().ToString();
}
catch(NullReferenceException n)
{
result = "";
}
}
ExecuteScaler gets you the first column of the first row and additional columns are ignored. Use the value from result in your SendKeys()
Use conditions to limit the result:
Select data
SELECT TOP 1 RequestID FROM AutomaticPayment // Always returns 1 row
Or
SELECT RequestID FROM AutomaticPayment WHERE Id = 123 // Id must be unique to return 1 row
And maybe other ways.
Get value
var value = dt.Rows[0][1];
Or
var value = dt.Rows[0]["RequestID"];
From what i worked on with SqlCommand just do the following :
int yourId = 0;
dataReader = command.ExecuteReader()
while(dataReader.Read())
{
yourId = dataReader.GetInt32(0);
}
With that, you should have your value set to the first column of the dataReader. (that is selected thanks to your query, since you are requesting on a specific id, i guess it will return only one column
there is many other type available for Reader : Reader Microsoft Doc
And if you have in the futur many data to collect, use the ORM entity framework, work well for me
Source
EDIT :
Since you are only querying one data, maybe the solution of #work_ishaan is better than mine in this case, check it out.

C# getAll Function advice

Hi I am trying to create CRUD functions in C# but am stuck on my first one which is FetchALL, as so far it says not all code path returns a value.
Heres my code so far
public SqlDataReader FetchAll(string tableName)
{
using (SqlConnection conn = new SqlConnection(_ConnectionString,))
{
string query = "SELECT * FROM " + tableName;
SqlCommand command = new SqlCommand(query, conn);
using (SqlDataReader reader = command.ExecuteReader())
conn.Open();
conn.Close();
}
}
}
}
I can give you more information, thanks
You have a return type of SqlDataReader, but you aren't returning anything anywhere in your code. At the very least you should declare your data reader and return it like this:
public SqlDataReader FetchAll(string tableName)
{
SqlDataReader reader;
using (SqlConnection conn = new SqlConnection(_ConnectionString))
{
string query = "SELECT * FROM " + tableName;
// added using block for your command (thanks for pointing that out Alex K.)
using (SqlCommand command = new SqlCommand(query, conn))
{
conn.Open(); // <-- moved this ABOVE the execute line.
reader = command.ExecuteReader(); // <-- using the reader declared above.
//conn.Close(); <-- not needed. using block handles this for you.
}
}
return reader;
}
Note, I've noted a few other problems I saw as well, which you can see by my comments.
Also, I want to point out something very important: you should always avoid string concatenation in queries as this opens you up to the risk of a SQL injection attack (as gmiley has duly pointed out). In this case, you should create an enum which contains values associated with all the possible table names, and then use a dictionary to look up the table names based on their enum values. If a user provides an invalid/unknown value, you would then thrown an argument exception.
This isn't the end of your problems, though (as Default has pointed out). You can't create the connection in a using block, which disposes and closes as soon as it exits the block, and then use the SqlDataReader that is returned from the method. If I were you, I'd return a DataSet instead of a SqlDataReader. Here's how I'd do it:
First, create your enum of possible table values:
public enum Table
{
FirstTable,
SecondTable
}
And a dictionary that maps table enum values to the table names (which you will populate in your static constructor):
private static Dictionary<Table, string> _tableNames = new Dictionary<Table, string>(); // populate this in your static constructor.
And then here is your method to fetch the data:
public static System.Data.DataSet FetchAll(Table fromTable)
{
var ret = new System.Data.DataSet();
using (var conn = new System.Data.SqlClient.SqlConnection(_connectionString))
{
string tableName = "";
if (!_tableNames.TryGetValue(fromTable, out tableName)) throw new ArgumentException(string.Format(#"The table value ""{0}"" is not known.", fromTable.ToString()));
string query = string.Format("SELECT * FROM {0}", tableName);
using (var command = new System.Data.SqlClient.SqlCommand(query, conn))
{
using (var adapter = new System.Data.SqlClient.SqlDataAdapter(command))
{
adapter.Fill(ret);
}
}
}
return ret;
}
One final note, I'd advise you name your class-level variables with lower camel case per convention, e.g. _connectionString.
Firstly you aren't returning anything from the method. I'd add, are you sure you want to return a SqlDataReader? It is declared within a using block, so it will be closed by the time you return it anyway. I think you should re-evaluate what this function should return.
You need a return statment for the method to return a value.

How to use foreach loop to edit some rows while populating datatable from sql?

I am trying to populate a data table from a database table. After it is populated i am using asp:GridView to show the data.
The problem is I am trying to edit some rows after it is populated from the database but before binding the data.
I am very new to c# so i cant get it working. My code does not update anything.
private void BindGridviewFileData()
{
string ConnectionStringName = WebConfigurationManager.ConnectionStrings["LocalConnection"].ConnectionString;
try
{
using (SqlConnection sqlConn = new SqlConnection(ConnectionStringName))
{
using (SqlCommand sqlCmd = new SqlCommand())
{
sqlCmd.CommandText = "SELECT * FROM VISUALISATION";
sqlCmd.Connection = sqlConn;
sqlConn.Open();
SqlDataReader objDataReader = sqlCmd.ExecuteReader();
DataTable objDataTable = new DataTable();
objDataTable.Load(objDataReader);
foreach (DataRow row in objDataTable.Rows)
if (row["lineNumber"] == "1")
row["BATCH_NO"] = "new Value";
gvSubjectDetails.DataSource = objDataTable;
gvSubjectDetails.DataBind();
sqlConn.Close();
}
}
}
catch { }
}
The lineNumber check probably needs to be an int check; note that currently it is actually doing an object check, which only supports reference equality - it will never match, even if the value is a string of "1", because the value from the database will become a different string instance than the interned literal "1" from the code. So, the first check probably needs to be:
if((int)row["lineNumber"] == 1)
You ask in comments how to do this with a string; in that case, quite similarly:
if((string)row["BATCH_NO"] == "some value")
This now works because there is an overloaded == operator for string==string, which does a value based equality test, not a reference-based equality test.
Note: you could also probably use:
if(Equals(row["BATCH_NO"], "some value"))
but I find the earlier version clearer.

C# exits method when I add item to string array

I'm struggling to understand why when I add an item to my string array "internalDives", the method exits and there is no debug error shown.
Please, what have I done wrong here?
private void GridDataConnection()
{
using (SqlCeConnection conn = new SqlCeConnection(ConnectionString))
{
conn.Open();
using (SqlCeCommand command = new SqlCeCommand("SELECT id,divelocation,divedate,diveduration FROM loggeddives", conn))
{
SqlCeDataReader readDiveResult = command.ExecuteReader();
var diveList = new List<string[]>();
while (readDiveResult.Read())
{
string[] internalDives = new string[4];
internalDives[0] = readDiveResult.GetString(0);
internalDives[1] = readDiveResult.GetString(1);
internalDives[2] = readDiveResult.GetString(2);
internalDives[3] = readDiveResult.GetString(3);
diveList.Add(internalDives);
i++;
}
}
conn.Close();
}
}
Are you sure all columns types are string? GetString doesn't make any conversion and suppose all columns are of type string. I suggest you to use a try catch statement for catching any InvalidCastException.
Check also that all values are instanced. You can do it with IsDBNull method.
The first thing you should do is absolutely to catch the exception that is being thrown, as mservidio mentioned in comments.
In all likelihood what you will discover when you do is that one of your calls to GetString is throwing an IndexOutOfBoundsException, so be prepared to step through your code carefully and look at what is coming down the Read pipeline. It might not be what you think it is.
I guess that you have null values in your database so, when you try to read one of the columns you get the error: "Data is Null. This method or property cannot be called on Null values". You must call the IsDBNull method to check if the value is null or not and after that read the value.
try this code
private void GridDataConnection()
{
using (SqlCeConnection conn = new SqlCeConnection(ConnectionString))
{
conn.Open();
using (SqlCeCommand command = new SqlCeCommand("SELECT id,divelocation,divedate,diveduration FROM loggeddives", conn))
{
SqlCeDataReader readDiveResult = command.ExecuteReader();
var diveList = new List<string[]>();
while (readDiveResult.Read())
{
string[] internalDives = new string[4];
internalDives[0] = readDiveResult.IsDBNull(0) ? "": readDiveResult.GetString(0);
internalDives[1] = readDiveResult.IsDBNull(1) ? "": readDiveResult.GetString(1);
internalDives[2] = readDiveResult.IsDBNull(2) ? "": readDiveResult.GetString(2);
internalDives[3] = readDiveResult.IsDBNull(3) ? "": readDiveResult.GetString(3);
diveList.Add(internalDives);
i++;
}
}
conn.Close();
}
}
I usually validate information coming back from a query before attempting to add it to an array or a list. Do a quick check to verify that all of the values are strings and perhaps even get a count to verify there are 4 values. As others have mentioned a try catch is quite helpful. You can also create a quick error log to dump any exceptions into a temp file.
First answer be gentle!

How do get a simple string from a database

The following code does not work. There is only 1 row in this table. How do I just get the statement that the sql would return?:
SqlConnection conn = new SqlConnection(connectionStringArg);
SqlCommand command = new SqlCommand("select applicationname from tbl_settings");
command.Connection = conn;
conn.Open();
string simpleValue = command.ExecuteReader()[0].ToString();
conn.Close();
return simpleValue;
Okay any help on how to achieve this relatively simple task would be great.
Since there's only a single value returned, you could do this:
string value = (string)command.ExecuteScalar();
If you need more than the first column from the first row returned, you'll need to use ExecuteReader(). See driis' answer.
The DataReader can't be indexed like that. You need something like:
using(conn)
using(var reader = command.ExecuteReader())
{
reader.Read();
string simpleValue = reader.GetString(0);
}
The general idea is to advance the reader for each record (using Read), then you can read values from the row. As pointed out in another answer, if you know there is only one single value in the result set, command.ExecuteScalar() gives you just that.
You have to call the Read method on a DataReader returned from ExecuteReader to get to the first row. Something like this:
SqlDataReader rdr = command.ExecuteReader();
if (rdr.Read())
...
You'd do e.g.
using(SqlConnection conn = new SqlConnection(connectionStringArg))
using(SqlCommand command = new SqlCommand("select applicationname from tbl_settings")) {
command.Connection = conn;
conn.Open();
SqlDataReader reader = command.ExecuteReader();
if(reader.read()) {
return reader.GetString(0);
}
return null;
}
For a single string you want to use ExecuteScalar to get a single value and then attempt to cast it to string.
From doc: Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
Example:
string result = command.ExecuteScalar() as string;
For small stuff I often find LINQ2SQL the easiest to set up and use. This tutorial should get you up and running in a few minutes. For bigger projects LINQ2SQL is considered obsolete, see this question for discussion and alternatives.
Check DataReader.Read() you need to call it.
string applicationname = "";
using(var reader = command.ExecuteReader())
{
reader.Read();
applicationname = reader.GetString(reader.GetOrdinal("applicationname"));
}
var query_result = com.ExecuteScalar() as string;
if (!string.IsNullOrEmpty(query_result )) // do your thing here...

Categories

Resources