Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
protected void Submitbtn_Click(object sender, EventArgs e)
{
   string connection_string = @"Provider=Microsoft.Jet.OLEDB.4.0;
   Data Source=C:\HILProject\project4\App_Data\adminlog.mdb";

   using (OleDbConnection cn = new OleDbConnection(connection_string))
   {
      cn.Open();

      String str = "select Nationality, Post from BasicDtl_tbl where UserName=" + usertxt.Text;
      OleDbCommand cmd = new OleDbCommand(str,cn);
      // cmd.Connection = cn;
      OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
      System.Data.DataSet ds = new System.Data.DataSet();
      try
      {
         ad.Fill(ds);
 
         Nattxt.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString();
         apldposttxt.Text = ds.Tables[0].Rows[0].ItemArray[4].ToString();
         cn.Close();
      }
      catch(Exception)
      {
         StatusLabel.Text = "error occured";
      }
   }
}


data does not get retrieved
Posted
Updated 4-Jun-14 1:49am
v3
Comments
Nirav Prabtani 4-Jun-14 7:21am    
what is error???
try to debug code.. :)
[no name] 4-Jun-14 7:34am    
You are missing single quotes for your user name. But you should be using parameterized queries instead as you have already been told.
Ajith K Gatty 4-Jun-14 9:14am    
You are reposted your question again with little changes. But your query is again wrong exactly like your previous post.Use ' and " properly. Check that last post please. Just fix it first. Then debug. Good Luck.

There are so many possibilities, but start with your SQL: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

In your case, any "odd" character in the username, such as a space will also cause problems!

The chances are your problem will be resolved by using paramaterized queries, but personally I'd do a sanity check to make sure that the DataAdapter actually returned at least one row of at least one table before I started using them...that might help you as well.
 
Share this answer
 
Comments
Ajith K Gatty 4-Jun-14 9:14am    
good explanation Sir!!
After the below line, while debugging, check if the DataSet actually contains data or not.

One more important thing is that you don't need to Open and Close the Connection when you use DataAdapter, as it implicitly Opens and Closes the Connection for you.
 
Share this answer
 
C#
protected void basic()
{
        bool temp = false;
        string connection_string = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\HILProject\project4\App_Data\adminlog.mdb";
        using (OleDbConnection cn = new OleDbConnection(connection_string))
        {
            cn.Open();
            String str = "select Nationality, Post from BasicDtl_tbl where UserName='" + usertxt.Text.Trim() + "'";
            OleDbCommand cmd = new OleDbCommand(str, cn);
            cmd.Connection = cn;
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Nattxt.Text = dr.GetString(0);
                apldposttxt.Text = dr.GetString(1);
                temp = true;
            }
            if (temp == false)
                StatusLabel.Text = "error occured";
            cn.Close();
        }
    }



[Edited]:Set Language Tag
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 22-Jun-14 0:39am    
Not an answer.
—SA

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