SQL Server - DataSet DataTable DataRow foreach loop

Asked By N
08-Apr-11 01:48 AM
string mySQL = "SELECT subject_id," + " message,phone_no from massMessage";

           SqlDataAdapter adapter = new SqlDataAdapter(mySQL, conn);
           DataSet ds = new DataSet();
           adapter.Fill(ds, "phone_no , message");

           DataTable tblNumbers = ds.Tables["phone_no"];
           DataRow drNum = null;
           DataTable tblMsg = ds.Tables["message"];
           DataRow drMsg = null;

           foreach (DataRow drNum_loopVariable in tblNumbers.Rows)
           {
               //send msg logic or send mail logic
               //with dr["number"].tostring()
               drNum = drNum_loopVariable;
               InsertSMSRecord(drNum["phone_no"].ToString());

                 foreach (DataRow drMsg_loopVariable in tblMsg.Rows) 
                 {
                     drMsg = drMsg_loopVariable;
                     InsertSMSRecord(drMsg["message"].ToString());

                 }
           }
Why is there error on those underlined?
  Reena Jain replied to N
08-Apr-11 01:54 AM
hi,

You can not assign the row directly without defining column in datatable. So try this

foreach (DataRow drNum_loopVariable in tblNumbers.Rows)
{
   //send msg logic or send mail logic
   //with dr["number"].tostring()
       
   InsertSMSRecord(drNum_loopVariable["phone_no"].ToString());
  
   foreach (DataRow drMsg_loopVariable in tblMsg.Rows)
   {
    InsertSMSRecord(drNum_loopVariable["message"].ToString());
   }
}

Hope this will help you
Create New Account
help
VB.NET / SQL Server SQL Server I???m using VB.NET 2008 and SQL Server 2005. I???m using the table ???tblEmployees??? from database ???DBMain???. The table look like the that? . . . Dim oVar as String oVar = SELECT Firstname FROM tblEmployees WHERE ID = ???2??? ??? Tanx; Nicolas SQL Server Programming Discussions SQL Server 2005 (1) SQL Server (1) Console.WriteLine (1) VB.NET (1
space planning for SQL Server SQL Server Hi I am looking for a script that will enumerate all the DBs on a sql server, show me the used MB and the max MB allocated per db. My intent would isn’t returning anything on it. Can someone aim me in the right direction? thanks SQL Server Discussions SQL Server (1) Exec (1) Disk (1) SqlDataAdapter (1) Database (1) AdventureWorks (1) SqlParameter
Import Data from Excel file to SQL server how to import data from excel file into the SQL server Use DTS or SSIS You can use the SQL Server Data Transformation Services (DTS) Import Wizard or the SQL Server Import and Export Wizard to import Excel data into SQL Server tables. Check this link
XML limits in SQL / Server? SQL Server I'm trying to apply techniques I'd normally use to pull data from SQL / Server to pull XML from SQL / Server, and I'm running into a problem. Here's what I'm doing: 1. I
C# and SQL Server: Parallel vs. Sequential Writes (?) SQL Server Consider: - My SQL Server has 8 processors (cores actually). - I want to upload 8, 000 records to a single done faster? (a) spawn 8 different threads in my client application and, using eight different sql connections, upload 1, 000 entries on each thread, or (b) upload all 8, 000 entries Option (a) would seem to make more effective use of all eight processors on the SQL Server. But in the final analysis, will not the SQL Server bottleneck be the transaction