Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help with reading data from my database. I have tried this piece of code but it doesnt seem to display the results in the text box i want. The 'filename' in the where clause is already defined. its just not incluced here

<pre>
String connection, sql;

connection = Properties.Settings.Default.cvmanagerConnectionString;
sql = "select FileLoc, Fname, Lname from apps where FileLoc = '" + filename + "'";

try
{
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();
reader = cmd.ExecuteReader();

if (reader.HasRows)
{
while (reader.Read())
{
tbappname.Text = reader["Fname"].ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Posted
Updated 28-Oct-10 1:03am
v3
Comments
Mohd Wasif 28-Oct-10 7:05am    
what is + filename +
Please clarify
OriginalGriff 28-Oct-10 7:09am    
What is it doing that it shouldn't or not doing that it should?
[no name] 28-Oct-10 7:13am    
filename is the location of the file on my computer. i am using openfiledialog to get the filename once a file is selected. it is this filename that i append to the where clause. this same filename is stored in the database with an earlier command.
[no name] 28-Oct-10 7:14am    
originalgriff.....it is suppose to display the a name in the textbox. this name is in my database in the same row as the filename. the filename acts as an id.
OriginalGriff 28-Oct-10 7:39am    
Ok. Is it replacing the tbappname.Text with anything? Or with blank? Or does it not change it at all?
Put a breakpoint on the "reader = cmd.ExecuteReader();" line, and run your program to there. Then single step though to see what it happening.

SqlCommand cmd = new SqlCommand("select FileLoc, Fname, Lname from apps where FileLoc = '" + TextBox1.Text + "'", conn);
conn.open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{

tbappname.Text = reader["Fname"].ToString();
reader.Close();
conn.Close();
}
else
{
string message="no record found";
reader.Close();
conn.Close();
}
 
Share this answer
 
Comments
[no name] 28-Oct-10 7:16am    
if you look at line 3, i have initialised the sql variable. it is this variable together with conn that i have parsed to the sqlcommand method. isnt this the same as at what you are suggesting?
If your planning to bind text in Textbox control please use the below code .. what your wrote the above code is to get all record from db and it will bind record one by one to TextBox control so the previous value will clear and new value will come to the pic.
Use the below code

String connection, sql;

connection = Properties.Settings.Default.cvmanagerConnectionString;
sql = "select FileLoc, Fname, Lname from apps where FileLoc = '" + filename + "'";

try
{
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();
//reader = cmd.ExecuteReader();

object Fname = cmd.ExecuteScalar();

if(Fname!=null)
{
tbappname.Text = Fname.ToString();
}
//if (reader.HasRows)
//{
//while (reader.Read())
// {
// tbappname.Text = reader["Fname"].ToString();
// }
//}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close()
}
 
Share this answer
 
v2
Comments
[no name] 28-Oct-10 7:26am    
it still does not work.
check do you have data in db for that particular file name...

please run the below code for testing weather the obeject returning data or not and let me know .

String connection, sql;

connection = Properties.Settings.Default.cvmanagerConnectionString;
sql = "select Fname from apps where FileLoc = '" + filename + "'";

try
{
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();
//reader = cmd.ExecuteReader();

object Fname = cmd.ExecuteScalar();

if(Fname!=null)
{
Response.Write("alert('"+ Fname.ToString() +"');");
tbappname.Text = Fname.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close()
}
 
Share this answer
 
Comments
[no name] 28-Oct-10 8:08am    
i solved the problem. i had to make sure the filename was pointing to the file folder and not the actual file so i reconfigured the database to that effect. thanks a lot though
use ExecuteScalar() instead of ExecuteReader()

string name=cmd.ExecuteScalar().ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900