Taking input

Discussion in 'C#' started by maher, Sep 24, 2010.

  1. maher

    maher New Member

    Joined:
    Sep 24, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    HEY im trying to take input from textbox and then i want to run a query in query in want to search that value
    suppose my textbox name is txtInput.text
    n query which i want to run


    Code:
    string connectionString = "Data Source =USER-PC\\SQLEXPRESS1;Initial Catalog=inFlowInventory; Integrated Security=SSPI";
                using (SqlConnection testConnection = new SqlConnection(connectionString))
                {
                    SqlCommand testCommand =
                        [B][COLOR="Red"]new SqlCommand("SELECT customer FROM Customer where customer =txtInput.Text", testConnection);[/COLOR][/B]
                    
                    testConnection.Open();
                    SqlDataReader sqlDr =testCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    if (sqlDr.HasRows)
                    {
                        foreach (DbDataRecord rec in sqlDr)
                        {
                            dbRecordHolder.Add(rec);
                           
                        }
                    }
                    CustomerNameGrid.DataSource = dbRecordHolder;
                  }
     
    Last edited by a moderator: Sep 24, 2010
  2. maher

    maher New Member

    Joined:
    Sep 24, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    lol
    wat happn no1 knw dat?? :P
     
  3. dotNet Zombie

    dotNet Zombie New Member

    Joined:
    Aug 28, 2010
    Messages:
    34
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    .Net Developers
    Location:
    Minnesota
    Home Page:
    http://www.dotnetzombie.net
    LOL actually the .Net Zombie was busy with work.
    Try setting up Parameters like this..
    Code:
     SqlCommand testCommand =new SqlCommand("SELECT customer FROM Customer where customer =@testCust", testConnection);
    
    testCommand.Parameters.AddWithValue("@testCust",txtInput.Text);
    
    
     
  4. maher

    maher New Member

    Joined:
    Sep 24, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Can u tel me 1 more thng i want to use Datagrid view as a Input of data n use of some qurey
    supose table column are item, descrptn, price, quantity
    i want to add 1 more Column which wil do da calculation lyke Total column which wrk iz to calculate da value in Run Tyme price*quantity as Total
    how can i perform that during insert query tyme
     
  5. dotNet Zombie

    dotNet Zombie New Member

    Joined:
    Aug 28, 2010
    Messages:
    34
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    .Net Developers
    Location:
    Minnesota
    Home Page:
    http://www.dotnetzombie.net
    Code:
    gridView.Rows[rowIndex].Cells["Calc"].Value=Convert.ToInt32(gridView.Rows[rowIndex].Cells["Price"].Value)*Convert.ToInt32(gridView.Rows[rowIndex].Cells["Quantity"].Value);
    to make the calculation on the grid itself, the rowIndex is the variable i made up to represent the selectedIndex of the row. (CurrentRow.Index)

    then during query time you can add gridView.Rows[rowIndex].Cells["Calc"].Value as a parameters.

    or you can

    Code:
    int Price=Convert.ToInt32(gridView.Rows[rowIndex].Cells["Price"].Value)
    int Quality=Convert.ToInt32(gridView.Rows[rowIndex].Cells["Quantity"].Value);
    and add the parameter like this

    Code:
    
    SQLCommand insertCommand=new SqlCommand("INSERT INTO tblTable(Total) VALUES(@Total)",sqlConn);
    insertCommand.Paramaters.AddWithValue("@Total",Price*Quality);
     
  6. maher

    maher New Member

    Joined:
    Sep 24, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    sorry em nt dat much expert can u do it easily or gve me complete codding if u can :(
     
  7. maher

    maher New Member

    Joined:
    Sep 24, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    thr is error showing in rowIndex
     
  8. dotNet Zombie

    dotNet Zombie New Member

    Joined:
    Aug 28, 2010
    Messages:
    34
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    .Net Developers
    Location:
    Minnesota
    Home Page:
    http://www.dotnetzombie.net
    rowIndex is a local variable which stores the GridView Controls property CurrentRow.Index.


    *sorry for taking a while to respond, have been busy*
     
    shabbir likes this.
  9. maher

    maher New Member

    Joined:
    Sep 24, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    thnx but can u merge awl thg
    n can u do it awl combine wid example
     
  10. dotNet Zombie

    dotNet Zombie New Member

    Joined:
    Aug 28, 2010
    Messages:
    34
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    .Net Developers
    Location:
    Minnesota
    Home Page:
    http://www.dotnetzombie.net
    Code:
    int rowIndex=gridView.CurrentRow.Index;
    int Price=Convert.ToInt32(gridView.Rows[rowIndex].Cells["Price"].Value)
    int Quality=Convert.ToInt32(gridView.Rows[rowIndex].Cells["Quantity"].Value);
    
    SQLCommand insertCommand=new SqlCommand("INSERT INTO tblTable(Total) VALUES(@Total)",sqlConn);
    insertCommand.Paramaters.AddWithValue("@Total",Price*Quality);
    
    insertCommand.Conn.Open();
    insertCommand.ExecuteNonReader();
    insertCommand.Conn.Close();
    
     
    shabbir and maher like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice