Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i need help on displaying database into textbox.i dont know how to do it.pls anyone help me.i already try coding from other sources but it return nothing or error.i dont know exactly the way of doing it.then after display, i want to update the data through textbox.

p/s: the data is starting from column 4th from database
Posted
Updated 13-Sep-17 1:27am
v2
Comments
manognya kota 29-Jan-12 23:32pm    
Hi,

could you please let us know what sources have you tried?

First of all please clear your question, need some more specification or enhancement about what you want ant what you are trying already..

as per my understanding about your question i replied it...

1) first of designed your asp.net page with number of textboxes you need to show the data like if you need to show 1 columns data then create or designed 1 textboxes with appropriate id or name...

2) in code behinde page you need to write the code for connecting to database and getting data from that database...

3) after getting data from database you need to bind that data with your text box..

4) as you mention that you need to update the data so you need to put one button for handaling updatation of database data...


i think you can perform 1st step with your own way.. and i here give a 1 column data binding example with my own textbox id, what you need is you need to modify my simple code in your own way and make appropriate changes for your solution...

for your database operation you can use code like this..
C#
protected void CreateConnection()
{
    try
    {
        if (SqlCon == null)
        {
            string ConnectionString = "YOUR CONNECTION STRING";
            SqlCon = new SqlConnection(ConnectionString);
        }
        if (Da == null)
        {
            Da = new SqlDataAdapter();
        }
    }
    catch (Exception exe)
    {
        throw exe;
    }
}

protected void OpenConnection()
{
    if (SqlCon != null && SqlCon.State != ConnectionState.Open)
    {
        SqlCon.Open();
    }
}
protected void CloseConnection()
{
    if (SqlCon.State != ConnectionState.Closed)
        SqlCon.Close();
}


for getting resulted data you can use like this..
C#
CreateConnection();
DataTable resultTable = new DataTable("YOURRESULTEDDATATABLENAME");
OpenConnection();
Da.SelectCommand = new SqlCommand();
Da.SelectCommand.Connection = SqlCon;
Da.SelectCommand.CommandType = CommandType.Text;
Da.SelectCommand.CommandText = "YOUR SELECT QUERY THAT WILL RETURN YOUR COLUMNS DATA";
Da.Fill(resultTable);

if (resultTable.Rows.Count > 0)
{
 // here i use only one record to disply so i give row number static that is 0 
    TextBox1.Text = resultTable.Rows[0]["YOUR COLUMN NAME"].ToString();
}
CloseConnection();


For update button query you need use something like this...
SQL
CreateConnection();
OpenConnection();
Da.UpdateCommand = new SqlCommand();
Da.UpdateCommand.Connection = SqlCon;
Da.UpdateCommand.CommandText = "YOUR UPDATE QUERY QUERY";
Da.UpdateCommand.ExecuteNonQuery();
CloseConnection();


i hope this will help you or further detail you can see this link..

Databinding[^]
 
Share this answer
 
v2
Comments
musiw 30-Jan-12 3:39am    
hi,

sorry to ask again..how to do it if i want to display in multiple texbox..?
it appear nothing in texbox.seem like no data is bind there
Tejas Vaishnav 30-Jan-12 3:45am    
if you need to bind data from only one column and many rows then only the row id will change with textbox id and if you bind the one row data with multipal columns then the column name will change with same row id

you can mange incremental row id with help of for loop etc...
musiw 30-Jan-12 5:29am    
i dont understand..can you show me the code.i already try that but it display not in correct way..

for instance if column [5] has 0 value it display any value.

SqlConnection myConnection = new SqlConnection(connectionString);
DataTable tbl = new DataTable("locomotive");
myConnection.Open();

string cmd2 = "Select avail,f_t,derated,shunting_feeder,pe,overhaul,repair from locomotive where id_class_loco = '" + ddlclass.SelectedIndex + "' and date = '" + TextBox1.Text + "' ";
SqlDataAdapter Ad = new SqlDataAdapter(cmd2, myConnection);
Ad.Fill(tbl);

//if (tbl.Rows.Count > 0)
//{
for (int r = 0; r < tbl.Rows.Count; r++)
{
avail.Text = tbl.Rows[r]["avail"].ToString();
ft.Text = tbl.Rows[r]["F_T"].ToString();
derated.Text = tbl.Rows[r]["derated"].ToString();
sf.Text = tbl.Rows[r]["shunting_feeder"].ToString();
pe.Text = tbl.Rows[r]["pe"].ToString();
overhaul.Text = tbl.Rows[r]["overhaul"].ToString();
repair.Text = tbl.Rows[r]["repair"].ToString();

}
//}
myConnection.Close();
musiw 31-Jan-12 4:11am    
its ok i ask someone else.nway thanks for your answer yesterday.
Hi,

Try binding the text box as below,

Method 1:
After you get the dataset,store the 4th column data in a variable and set the value of the text box.

txtBox_description.Text = String


Method 2:
Bind the column of the dataset to the textbox.

yourtxbxid.DataBindings.Add("Text",dsYourDataSet,"Datasetname.ColumnName");


Please have a look at the below link for example.

http://www.java2s.com/Code/CSharp/Database-ADO.net/BindDataSettoTextBox.htm[^]

Hope this helps.
 
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