Click here to Skip to main content
15,921,295 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can anyone just tell me what is the syntax to bind dataset data to textboxs in c# windows form.

C#
int temp = Form2.variable1;
           string s = System.Configuration.ConfigurationSettings.AppSettings.Get("Conn");
           SqlConnection connection = new SqlConnection(s);
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = connection;
           cmd.CommandType = CommandType.StoredProcedure;
           connection.Open();
           cmd.CommandText = "ssp_AgriShowdata";
           cmd.Parameters.AddWithValue("@temp", temp);
          SqlDataAdapter adapt = new SqlDataAdapter(cmd);

           DataSet ds = new DataSet();
           adapt.Fill(ds);

I am using this syntax to bind dataset value to textbox but it showing some exception.

C#
cFirstName.DataBindings.Add("Text", ds.Tables["Cultivator_tbl"], "c_FirstName");


ERROr:Value cannot be null.
Parameter name: dataSource
Posted
Updated 29-Jul-13 22:49pm
v2

C#
sconn.Open();
               string query = "select * from Student";
               SqlCommand cmd = new SqlCommand(query, sconn);
               SqlDataReader dr = cmd.ExecuteReader();

               DataTable dt = new DataTable();
               dt.Load(dr);
               txtFirstName.Text = dt.Rows[0][2].ToString();
               cmd.ExecuteReader();
               sconn.Close();
 
Share this answer
 
Comments
Rakhil Naik 30-Jul-13 4:51am    
thanks
I am using this syntax
cFirstName.DataBindings.Add("Text", ds.Tables["Cultivator_tbl"], "c_FirstName");
is there any other syntax?
Set the last FormattingEnabled Overload to true

C#
cFirstName.DataBindings.Add("Text", ds.Tables["Cultivator_tbl"], "c_FirstName",True);
 
Share this answer
 
v4
Comments
Rakhil Naik 30-Jul-13 4:59am    
thanks
but still it showing same Exception
ERROr:Value cannot be null.
Parameter name: dataSource
It will be easy and better if you modify these lines
C#
SqlDataAdapter adapt = new SqlDataAdapter(cmd);            
DataSet ds = new DataSet();
adapt.Fill(ds);

with this one
C#
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
    string temp=dr.GetValue(0).ToString();
    dr.Close();
}

Regards..:laugh:
 
Share this answer
 
int temp = Form2.variable1;
string s = System.Configuration.ConfigurationSettings.AppSettings.Get("Conn");
SqlConnection connection = new SqlConnection(s);
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = CommandType.StoredProcedure;
connection.Open();
cmd.CommandText = "ssp_AgriShowdata";
cmd.Parameters.AddWithValue("@temp", temp);
SqlDataAdapter adapt = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
adapt.Fill(ds);

//Use this code
if(ds!=null)
{
if(ds.Tables.Count>0)
{
if(ds.Tables["TableName"]!=null AND ds.Tables["TableName"].Rows.COunt>0)
{
TextBoxID=ds.Tables["TableName"].Rows[0]["ColumnName"].ToString();
}
}
}
 
Share this answer
 
try this

C#
objCon.Open();
              strQuery = "select intDataId from tbtrnTranscriptDataNew where vchEventId='" + strEventId + "' and intPartno=" + intPartno + " and intPos=" + intPos + " and intSection=" + intSection;
              SqlDataAdapter da1 = new SqlDataAdapter(strQuery, objCon);

              DataTable DataTabledata = new DataTable("data");
              da1.Fill(DataTabledata);

              DataColumn dc2 = DataTabledata.Columns["intDataId"];
              if (DataTabledata.Rows.Count > 0)
              {
                  foreach (DataRow row in DataTabledata.Rows)
                  {
                      txttextbox.text=row[dc2].tostring();
                  }

              }

              objCon.Close();
 
Share this answer
 
if(ds.tables["Cultivator_tbl"].rows.count>0
textbox.text=ds.tables["Cultivator_tbl"].Rows[0]["c_FirstName"].tostring();

It will help you
 
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